BUAA-SCSE Training day2

好多题目是uva上的
然后当时看过刘汝佳的书
再看看就好
还有一些思路都很清晰
代码也很少
就没有什么可写的了

A - Open Credit System
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

Problem E
Open Credit System
Input: 
Standard Input

Output: Standard Output

In an open credit system, the students can choose any course they like, but there is a problem. Some of the students are more senior than other students. The professor of such a course has found quite a number of such students who came from senior classes (as if they came to attend the pre requisite course after passing an advanced course). But he wants to do justice to the new students. So, he is going to take a placement test (basically an IQ test) to assess the level of difference among the students. He wants to know the maximum amount of score that a senior student gets more than any junior student. For example, if a senior student gets 80 and a junior student gets 70, then this amount is 10. Be careful that we don't want the absolute value. Help the professor to figure out a solution.

Input
Input consists of a number of test cases T (less than 20). Each case starts with an integer n which is the number of students in the course. This value can be as large as 100,000 and as low as 2. Next n lines contain n integers where the i'th integer is the score of the i'th student. All these integers have absolute values less than 150000. If i < j, then i'th student is senior to the j'th student.

Output
For each test case, output the desired number in a new line. Follow the format shown in sample input-output section.

Sample Input                             Output for Sample Input

3
2
100
20
4
4
3
2
1
4
1
2
3
4
 

80
3
-1


Problemsetter: Mohammad Sajjad Hossain

Special Thanks: Shahriar Manzoor

 

#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		int ans=-INF;
		int now;
		int last=-INF;
		cin>>n;
		while(n--)
		{
			cin>>now;
			ans=max(ans,last-now);
			last=max(last,now);
		}
		cout<<ans<<endl;
	}
//	system("pause");
	return 0;
}

B - Age Sort
Time Limit:5000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

B

Age Sort

Input: Standard Input

Output: Standard Output

 

You are given the ages (in years) of all people of a country with at least 1 year of age. You know that no individual in that country lives for 100 or more years. Now, you are given a very simple task of sorting all the ages in ascending order.

 
Input

There are multiple test cases in the input file. Each case starts with an integer (0<n<=2000000), the total number of people. In the next line, there are integers indicating the ages. Input is terminated with a case where = 0. This case should not be processed.

 

Output

For each case, print a line with space separated integers. These integers are the ages of that country sorted in ascending order.

 

Warning: Input Data is pretty big (~  25 MB) so use faster IO.

 

Sample Input                             Output for Sample Input

5

3 4 2 1 5

5

2 3 2 3 1

0

1 2 3 4 5

1 2 2 3 3

Note: The memory limit of this problem is 2 Megabyte Only.


Problem Setter: Mohammad Mahmudur Rahman

Special Thanks: Shahriar Manzoor

 

 


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int a[2000005];
int main()
{
	int T;
	int n;
	while(scanf("%d",&n)!=EOF&&n!=0)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		sort(a,a+n);
		for(int i=0;i<n-1;i++)
		{
			printf("%d ",a[i]);
		}
		printf("%d\n",a[n-1]);
	}
//	system("pause");
	return 0;
}

C - Spreading the Wealth
Time Limit:6000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF


 F. Spreading the Wealth 

Problem

A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins. Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.

The Input

There is a number of inputs. Each input begins with n(n<1000001), the number of people in the village. n lines follow, giving the number of coins of each person in the village, in counterclockwise order around the table. The total number of coins will fit inside an unsigned 64 bit integer.

The Output

For each input, output the minimum number of coins that must be transferred on a single line.

Sample Input

3
100
100
100
4
1
2
5
4

Sample Output

0
4

/*
//刘汝佳书上的,回去看 

原来金币数为a1,a2,`````an.
最终的金币为这些数的平均值,设为A
xi表示i给i+1的金币个数
a1+xn-x1=A
a2+x1-x2=A
a3+x2-x3=A
a4+x3-x4=A
.......
an+x(n-1)-xn=A

利用后面n-1个等式,用x1表示 x2,x3,...xn
x2=x1-(A-a2)
x3=x1-(2*A-a2-a3);
....

结果就是求|x1|+|x1-b1|+|x1-b2|+...+|x1-b[n-1]|的最小值,取中位数
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const int MAXN=1000005;
long long a[MAXN],b[MAXN];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        long long sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        long long A=sum/n;
        b[0]=0;
        for(int i=1;i<n;i++)
        {
			b[i]=b[i-1]+A-a[i+1];
		}
        sort(b,b+n);
        long long t=b[n/2];
        long long ans=0;
        for(int i=0;i<n;i++)
		{
			ans+=abs(t-b[i]);
		}
        printf("%lld\n",ans);
    }
//    system("pause");
    return 0;
}

D - Piotr's Ants
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

Problem D
Piotr's Ants
Time Limit: 2 seconds

"One thing is for certain: there is no stopping them;
the ants will soon be here. And I, for one, welcome our
new insect overlords."
Kent Brockman

Piotr likes playing with ants. He has n of them on a horizontal pole L cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end up Tseconds from now.

Input
The first line of input gives the number of cases, NN test cases follow. Each one starts with a line containing 3 integers: L , T and n (0 <=  n <= 10000)  . The next n lines give the locations of the n ants (measured in cm from the left end of the pole) and the direction they are facing (L or R).

Output
For each test case, output one line containing "Case #x:" followed by n lines describing the locations and directions of the n ants in the same format and order as in the input. If two or more ants are at the same location, print "Turning" instead of "L" or "R" for their direction. If an ant falls off the pole beforeT seconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample InputSample Output
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R


Problemsetter: Igor Naverniouk
Alternate solutions: Frank Pok Man Chu and Yury Kholondyrev

/*
还是刘汝佳书上的,也回去看
长l厘米的木棍上有n只蚂蚁,蚂蚁要么向左爬,要么向右爬,速度1cm/s。当两只蚂蚁碰面时,同时掉转方向。给出每只蚂蚁的初始朝向和位置,求t秒后每只蚂蚁位置。

蚂蚁相遇掉头后其实就是相穿而过。但是蚂蚁的编号就搞乱了,因此一开始要编号每只蚂蚁。着重理解好蚂蚁开始和结束的位置。。
*/
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 10005
struct str
{
  int x , d , id;
  bool operator < (const str& a) const
  {
    return x < a.x;   
  }
}ant[N] , run[N];

int n , T, ans , f[N] , t , L , ca;

int main()
{
  int i , x , y;  char c;
  cin >> T;
  while (T --)
  {
    cin >> L >> t >> n;
    printf("Case #%d:\n", ++ ca);
    for (i = 1 ; i <= n ;i ++)
    {
      scanf("%d %c\n",&x,&c);
      y = c == 'L' ? -1 : 1;  
      ant[i] = (str) {x , y , i};
      run[i] = (str) {x + t * y , y , i};
    }
    sort(ant + 1, ant + n + 1);
    sort(run + 1, run + n + 1);
    for (i = 1 ; i <= n ;i ++)
      f[ant[i].id] = i;
    for (i = 1 ; i < n ;i ++)
      if (run[i].x == run[i + 1].x)
        run[i].d = run[i + 1].d = 0;  
    for (i = 1 ; i <= n ;i ++)     
    {
      x = f[i];
      if (run[x].x < 0 || run[x].x > L)
        printf("Fell off\n");
      else
      {
        printf("%d ",run[x].x);  
        if (run[x].d == -1)
          printf("L\n");
        if (run[x].d == 0)
          printf("Turning\n");
        if (run[x].d == 1)
          printf("R\n");    
      }  
    } 
    printf("\n");   
  }  
  return 0;
}

E - Pie
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

Problem C - Pie

Time limit: 1 second

 My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.

Input

One line with a positive integer: the number of test cases. Then for each test case:

  • One line with two integers N and F with 1 ≤ N, F ≤ 10000: the number of pies and the number of friends.
  • One line with N integers ri with 1 ≤ ri ≤ 10000: the radii of the pies.

Output

For each test case, output one line with the largest possible volume  V such that me and my friends can all get a pie piece of size  V. The answer should be given as a floating point number with an absolute error of at most 10 -3.

Sample Input

3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2

Sample Output

25.1327
3.1416
50.2655
The 2006 ACM Northwestern European Programming Contest

/*
大意是:分馅饼,注意,每人(包括自己)有且只有一块,即每人的饼不能是由几块组合而成。。。。。求最大的体积,因为高是1,其实就是求做大的面积。。。
 
设最大的面积为x(即每人拿到的)。。。。转化成方程:a[1]/x + a[2]/x + ..... + a[n]/x =  y + 1
其中a[i]/x指各块馅饼最多能分给几个人。。。。由于n是变化的。。。。。所以不能直接像方程那样做。。。。。
而写出一个函数。。。。。。。。。。。注意代码中的函数中的if (count >= y + 1),为什么不是count  == y + 1呢?
因为不一定能等于y+1(除非特例),而我们求的解也只是近似解而已。。。。。。。而我们希望他接近y + 1,所以后面成立的时候
l = mid 。。。。。。即x升,y+1降。。。。。。
 
 
还有一点就是EPS不能太小。。。。不然超时。。。。。他保留4位,我们就计算到6(4+2)位就可以了。。。。
别忘了计算面积。。。。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath> 
#define MIN 1e-6
using namespace std;
const double PI = acos(-1.0);
int n,f;
int r[10005];
int ok(double x)
{
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		cnt+=int(r[i]*r[i]*PI/x);
	}
	if(cnt>=f)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>f;
		f+=1;
		double sum=0;
		for(int i=1;i<=n;i++)
		{
			cin>>r[i];
			sum+=r[i]*r[i]*PI;
		}
		double right=sum/f;
		double left=0;
		double mid;
		while(right-left>MIN)
		{
			mid=(right+left)/2;
			if(ok(mid))
			{
				left=mid;
			}
			else
			{
				right=mid;
			}
		}
		mid=(left+right)/2;
		printf("%.4lf\n",mid);
	}
	return 0;
}
			
		

F - Subsequence
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000)are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

Input

Many test cases will be given. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

Output

For each the case the program has to print the result on separate line of the output file. If there isn't such a subsequence, print 0 on a line by itself.

Sample Input

10 15 
5 1 3 5 10 7 4 9 2 8 
5 11 
1 2 3 4 5

Sample Output

2 
3

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int num[100005],sum[100005];
int n,s;

int search(int i)
{
    int left,right,mid;
    left=0,right=i;
    while(left<=right)
	{
        mid=(left+right)>>1;
        if(sum[i]-sum[mid]>=s)
           left=mid+1;
        else
           right=mid-1;
    }
    return i-right; 
}

int solve()
{
    int ans=INF;
    for(int i=1;i<=n;i++)
	{
        if(sum[i]>=s)
           ans=min(ans,search(i));
    }
    return ans==INF?0:ans;
}

int main()
{
    while(cin>>n>>s)
	{
         memset(sum,0,sizeof(sum)); 
         for(int i=1;i<=n;i++)
		 {
             cin>>num[i]; 
             sum[i]=num[i]+sum[i-1]; 
         }
         cout<<solve()<<endl; 
    }
    return 0;
}

G - Prime Matrix
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

You've got an n × m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.

You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not.

A matrix is prime if at least one of the two following conditions fulfills:

  • the matrix has a row with prime numbers only;
  • the matrix has a column with prime numbers only;

Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 500) — the number of rows and columns in the matrix, correspondingly.

Each of the following n lines contains m integers — the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.

The numbers in the lines are separated by single spaces.

Output

Print a single integer — the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.

Sample Input

Input
3 3
1 2 3
5 6 1
4 4 1
Output
1
Input
2 3
4 8 8
9 2 9
Output
3
Input
2 2
1 3
4 2
Output
0

Hint

In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.

In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.

In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.


/*
筛法都不会写了我太渣了。。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int map[505][505];
int prime[100005];
int n,m;
void init()
{
	int k=1;
	memset(prime,1,sizeof(prime));
	prime[0]=0;
	prime[1]=0;
	for(int i=2;i<=100005;i++)
	{
		if(prime[i])
		{
			for(int j=i+i;j<=100005;j+=i)
			{
				prime[j]=0;
			}
		}
	}
}
int find(int x)
{
	int i=x+1;
	for( ;!prime[i];i++)
	{
	}
	return i-x;
}
int main()
{
	init();
	while(cin>>n>>m)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				int e;
				cin>>e;
				if(!prime[e])
				{
					map[i][j]=find(e);
				}
				else
				{
					map[i][j]=0;
				}
			}
		}
		int ans=INF;
		for(int j=0;j<m;j++)
		{
			int tempans=0;
			for(int i=0;i<n;i++)
			{
				tempans+=map[i][j];
			}
			ans=min(tempans,ans);
		}
		for(int i=0;i<n;i++)
		{
			int tempans=0;
			for(int j=0;j<m;j++)
			{
				tempans+=map[i][j];
			}
			ans=min(tempans,ans);
		}
		cout<<ans<<endl;
	}
	return 0;
}
		 

H - Dividing Orange
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.

There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.

Now the children wonder, how to divide the orange so as to meet these conditions:

  • each child gets exactly n orange segments;
  • the i-th child gets the segment with number ai for sure;
  • no segment goes to two children simultaneously.

Help the children, divide the orange and fulfill the requirements, described above.

Input

The first line contains two integers nk (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.

It is guaranteed that all numbers ai are distinct.

Output

Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second nintegers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.

You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.

Sample Input

Input
2 2
4 1
Output
2 4 
1 3 
Input
3 1
2
Output
3 2 1 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> 
using namespace std;
int cnt[905];
int visit[905];
int n,k;
int main()
{
	while(cin>>n>>k)
	{
		memset(cnt,0,sizeof(cnt));
		memset(visit,0,sizeof(visit));
		for(int i=0;i<k;i++)
		{
			cin>>cnt[i];
			visit[cnt[i]]=1;
		}
		int j=1;
		for(int i=0;i<k;i++)
		{
			cout<<cnt[i]<<" ";
			int sum=1;
			for(; ;j++)
			{
				if(!visit[j])
				{
					cout<<j<<" ";
					visit[j]=1;
					sum++;
				}
				if(sum==n)
					break;
			}
			cout<<endl;
		}
	}
	return 0;
}

I - Books
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the i-th book.

Valera decided to choose an arbitrary book with number i and read the books one by one, starting from this book. In other words, he will first read book number i, then book number i + 1, then book number i + 2 and so on. He continues the process until he either runs out of the free time or finishes reading the n-th book. Valera reads each book up to the end, that is, he doesn't start reading the book if he doesn't have enough free time to finish reading it.

Print the maximum number of books Valera can read.

Input

The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.

Output

Print a single integer — the maximum number of books Valera can read.

Sample Input

Input
4 5
3 1 2 1
Output
3
Input
3 3
2 2 3
Output
1
#include<iostream>
using namespace std;
int book[100005];
int n,t;
int main()
{
	while(cin>>n>>t)
	{
		for(int i=0;i<n;i++)
		{
			cin>>book[i];
		}
		int sum=0;
		int first=0;
		int books=0;
		int ans=0;
		for(int i=0;i<n;i++)
		{
			if(sum+book[i]>t)
			{
				i--;
				sum-=book[first];
				books--;
				first++;
			}
			else
			{
				sum+=book[i];
				books++;
				ans=max(books,ans);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
	

J - Dice Tower
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).

Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).

Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.

The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.

Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.

Output

Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).

Sample Input

Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO

直接拿昂神代码了。。。
#include<iostream>
using namespace std;
int n;
int work(int a,int b)
{
  if ((a == 1 || a == 2 || a == 6 || a == 5) && (b == 1 || b == 2 || b == 6 || b == 5))
    return 3;
  if ((a == 1 || a == 3 || a == 6 || a == 4) && (b == 1 || b == 3 || b == 6 || b == 4))
    return 2;
  if ((a == 3 || a == 2 || a == 5 || a == 4) && (b == 3 || b == 2 || b == 5 || b == 4))
    return 1;    
}
int main()
{
	int i,x,y,a,b;  
  	cin>>n>>x;
  	for(i=1;i<=n;i++)
  	{
	  	cin>>a>>b;
    	y=work(a,b);
    	if(y!=x&&y!=7-x)
      		break;
  	}  
  	if(i<=n)
    	cout<<"NO"<<endl;
  	else 
	  	cout<<"YES"<<endl;
  	return 0;  
}

K - Building Permutation
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.

You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integersa1, a2, ..., an ( - 109 ≤ ai ≤ 109).

Output

Print a single number — the minimum number of moves.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Sample Input

Input
2
3 0
Output
2
Input
3
-1 -1 2
Output
6

Hint

In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is(2, 1).

In the second sample you need 6 moves to build permutation (1, 3, 2).


#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[300005];
int main()
{
	cin>>n;
	long long ans=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		ans+=abs(a[i]-i);
	}
	cout<<ans<<endl;
	return 0;
}

L - Dragon of Loowater
Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

Problem C: The Dragon of Loowater

Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.

The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predators, the geese population was out of control. The people of Loowater mostly kept clear of the geese. Occasionally, a goose would attack one of the people, and perhaps bite off a finger or two, but in general, the people tolerated the geese as a minor nuisance.

One day, a freak mutation occurred, and one of the geese spawned a multi-headed fire-breathing dragon. When the dragon grew up, he threatened to burn the Kingdom of Loowater to a crisp. Loowater had a major problem. The king was alarmed, and called on his knights to slay the dragon and save the kingdom.

The knights explained: "To slay the dragon, we must chop off all its heads. Each knight can chop off one of the dragon's heads. The heads of the dragon are of different sizes. In order to chop off a head, a knight must be at least as tall as the diameter of the head. The knights' union demands that for chopping off a head, a knight must be paid a wage equal to one gold coin for each centimetre of the knight's height."

Would there be enough knights to defeat the dragon? The king called on his advisors to help him decide how many and which knights to hire. After having lost a lot of money building Mir Park, the king wanted to minimize the expense of slaying the dragon. As one of the advisors, your job was to help the king. You took it very seriously: if you failed, you and the whole kingdom would be burnt to a crisp!

Input Specification:

The input contains several test cases. The first line of each test case contains two integers between 1 and 20000 inclusive, indicating the number n of heads that the dragon has, and the number m of knights in the kingdom. The next n lines each contain an integer, and give the diameters of the dragon's heads, in centimetres. The following m lines each contain an integer, and specify the heights of the knights of Loowater, also in centimetres.

The last test case is followed by a line containing:

0 0

Output Specification:

For each test case, output a line containing the minimum number of gold coins that the king needs to pay to slay the dragon. If it is not possible for the knights of Loowater to slay the dragon, output the line:

Loowater is doomed!

Sample Input:

2 3
5
4
7
8
4
2 1
5
5
10
0 0

Output for Sample Input:

11
Loowater is doomed!

Ondřej Lhoták

/*
排序贪心
刘汝佳书上的
*/
#include<iostream>
#include<algorithm>
using namespace std;
int a[200005];
int b[200005];
int n,m;
int main()
{
	while(cin>>n>>m,n!=0&&m!=0)
	{
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		for(int i=0;i<m;i++)
		{
			cin>>b[i];
		}
		sort(a,a+n);
		sort(b,b+m);
		int j=0;
		int ans=0;
		int i;
		for(i=0;i<n;i++)
		{
			if(j>=m)
			{
				break;
			}
			while(j<m&&a[i]>b[j])
			{
				j++;
			}
			if(j>=m)
			{
				break;
			}
			ans+=b[j];
			j++;
		}
		if(i<n)
		{
			cout<<"Loowater is doomed!"<<endl;
		}
		else
		{
			cout<<ans<<endl;
		}
	}
	return 0;
}
		

先睡觉去了
剩下的改天累了再整理


躁起来吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值