Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Nothing special, just some corner cases

n = int(input())
arr = [int(i) for i in input().strip().split()]

# Collect 
rec = []
i = 0
while i < n - 1:
    if arr[i] > arr[i + 1]:
        inx = i
        cnt = 1
        while i < n - 1 and arr[i] > arr[i + 1]:
            cnt += 1
            i += 1
        rec.append((inx, cnt))
    else:
        i += 1

# Check
if len(rec) == 1:
    if rec[0][1] == 2:
        if rec[0][0] + 1 < n - 1 and arr[rec[0][0]] > arr[rec[0][0] + 2]:
            print ("no")
        else:
            print ("yes")
            print ("swap %d %d" % (rec[0][0] + 1, rec[0][0] + 2))
    elif rec[0][1] > 2:
        if rec[0][0] + rec[0][1] - 1 < n - 1 and arr[rec[0][0]] > arr[rec[0][0] + rec[0][1]]:
            print ("no")
        else:
            print ("yes")
            print ("reverse %d %d" % (rec[0][0] + 1, rec[0][0] + rec[0][1]))
elif len(rec) == 2 and rec[0][1] == 2 and rec[1][1] == 2:
    print ("yes")
    print ("swap %d %d" % (rec[0][0] + 1, rec[1][0] + 2))
else:
    print ("no")
posted on 2015-03-18 03:01  Tonix  阅读(254)  评论(0编辑  收藏  举报