# SPOJ COWPIC

591人阅读 评论(0)

E - Cow Photographs
Time Limit:177MS     Memory Limit:1572864KB     64bit IO Format:%lld & %llu

Description

Farmer John wants to take a picture of his entire herd of N (1 <= N <= 100,000) cows conveniently numbered 1..N so he can show off to his friends.

On picture day, the cows run to form a single line in some arbitrary order with position i containing cow c_i (1 <= c_i <= N). Farmer John has his own ideas about how the cows should line up.

FJ thinks cow i may stand only to the left of cow i+1 (for all i, 1 <= i <= N-1) and that cow N may only stand to the left of Cow 1.  Of course, no cow will stand to the left of the first (leftmost) cow in the line.

The cows are hungry for the promised post-photo dinner, so Farmer John wants to take the picture as quickly as possible. Cows are not great at following directions, so he will only choose a pair of adjacent cows and have them switch places once per minute. How quickly is Farmer John able to get them into some acceptable order?

Consider a set of 5 cows whose initial lineup looks like this:

Left           Right
3  5  4  2  1

He can first swap the second pair of cows:

3  4  5  2 1

and then swap the rightmost pair:

3  4  5  1  2

to yield an acceptable lineup that required but two minutes of cow swapping.

### Input

Line 1: A single integer: N
Lines 2..N+1: Line i+1 contains the number of the i-th cow in line: c_i

### Output

Line 1: The minimum amount of time, in minutes, that it takes Farmer John to get the cows into some appropriate order.

### Example

Input:
5
3
5
4
2
1

Output:
2

#include <stdio.h>
#include <iostream>
using namespace std;
long long int g_nCount=0;
void mergearray(int a[], int first, int mid, int last, int temp[])
{
int i = first, j = mid + 1;
int m = mid,   n = last;
int k = 0;

while (i <= m && j <= n)
{
if (a[i] < a[j])
temp[k++] = a[i++];
else
{
temp[k++] = a[j++];
g_nCount += m - i + 1;
}
}

while (i <= m)
temp[k++] = a[i++];
while (j <= n)
temp[k++] = a[j++];

for (i = 0; i < k; i++)
a[first + i] = temp[i];
}
void mergesort(int a[], int first, int last, int temp[])
{
if (first < last)
{
int mid = (first + last) / 2;
mergesort(a, first, mid, temp);
mergesort(a, mid + 1, last, temp);
mergearray(a, first, mid, last, temp);
}
}

bool MergeSort(int a[], int n)
{
int *p = new int[n];
if (p == NULL)
return false;
mergesort(a, 0, n - 1, p);
return true;
}

int main()
{

int n;
long long int minn;
int store[100005],p[100005];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>store[i];
}
for(int i=0;i<n;i++)
{
p[store[i]]=i;
}
MergeSort(store, n);
g_nCount=g_nCount-p[1]+(n-p[1]-1);
minn=g_nCount;
for(int i=2;i<n;i++)
{
g_nCount=g_nCount-p[i]+(n-p[i]-1);
if(g_nCount<minn)
minn=g_nCount;
}
printf("%lld\n",minn);
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：208546次
• 积分：5370
• 等级：
• 排名：第5069名
• 原创：321篇
• 转载：9篇
• 译文：0篇
• 评论：43条
博客专栏
 从0开始刷LeetCode，备战面试——EasyVersion 文章：10篇 阅读：2455
 多校题集 文章：38篇 阅读：24452
 刷PAT，申Offer 文章：24篇 阅读：19779
 【打CF，学算法】 文章：49篇 阅读：33975
阅读排行