SPOJ COWPIC

原创 2015年07月07日 23:28:50

题目链接:COWPIC


题面:

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


解题:

听了别人的思路做出来的。逆序数对就是需要移动到1 2 3.... n的次数,而不断将第1位移到最后,将其当成最大的数,减去其左边的位置数,加上总数减去其位置数再减一,便得一新值,将新值与最小值比较得最优解。(前面得逆序数对数需用归并排序,nlogn,用n^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;
}


版权声明:欢迎交流!

SPOJ DQUERY (离线数状数组||在线主席树)

转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题目:给出一个序列,查询区间内有多少个不同的...
  • ACM_cxlove
  • ACM_cxlove
  • 2013年02月01日 12:59
  • 3142

SPOJ375——Query on a tree(树链剖分模板详解以及入门)

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
  • say_c_box
  • say_c_box
  • 2016年08月05日 10:42
  • 1430

SPOJ COT 树上第K大(LCA+主席树)

转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题目:给出一棵树,问两个节点之间路径经过的结...
  • ACM_cxlove
  • ACM_cxlove
  • 2013年02月01日 20:24
  • 4077

spoj4491 莫比乌斯反演

  • 2015年08月22日 12:48
  • 263KB
  • 下载

spoj做题表格

  • 2012年10月21日 19:58
  • 133KB
  • 下载

spoj 第42题 reverse

  • 2009年03月30日 14:45
  • 413B
  • 下载

SPOJ极少量的源程序。

  • 2010年03月30日 17:51
  • 14KB
  • 下载

【模板】【SPOJ375】树链剖分

【模板】树链剖分->基于边的重编号
  • sdfzchy
  • sdfzchy
  • 2017年07月15日 15:29
  • 91

SPOJ D-query 区间不同数的个数 [在线主席树 or 离线树状数组]

给出N个数,M个询问,每次询问给出区间内不同数的个数。
  • GrassTreeFlower
  • GrassTreeFlower
  • 2016年03月03日 09:56
  • 835

SPOJ COT2(树上莫队)

You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer w...
  • creatorx
  • creatorx
  • 2017年11月06日 16:22
  • 78
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SPOJ COWPIC
举报原因:
原因补充:

(最多只允许输入30个字)