首页 > 代码库 > ------2014-12-14
------2014-12-14
今天神马都没干、意思一下。
SWUST OJ 365
均分纸牌
问题描述:
有 N 堆纸牌,编号分别为 1,2,…, N。每堆上有若干张,但纸牌总数必为 N 的倍数。可以在任一堆上取若于张纸牌,然后移动。
移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 的堆上;在编号为 N 的堆上取的纸牌,只能移到编号为 N-1 的堆上;其他堆上取的纸牌,可以移到相邻左边或右边的堆上。
现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多。
例如 N=4,4 堆纸牌数分别为:
① 9 ② 8 ③ 17 ④ 6
移动3次可达到目的:
从 ③ 取 4 张牌放到 ④ (9 8 13 10) -> 从 ③ 取 3 张牌放到 ②(9 11 10 10)-> 从 ② 取 1 张牌放到①(10 10 10 10)。
输入:
N(N 堆纸牌,1 <= N <= 100)
A1 A2 … An (N 堆纸牌,每堆纸牌初始数,l<= Ai <=10000)
输出:
所有堆均达到相等时的最少移动次数。
样例输入:
4
9 8 17 6
样例输出:
3
最开始还想复杂了、直接O(n)依次判断每一个与平均数的大小、少于则从后面拿、多余则把多的给后面。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;#define N 1010int n;int a[N];int main(){ int ave,sum=0,ans=0; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum+=a[i]; } ave=sum/n; for(int i=1;i<=n;i++) { if(a[i]!=ave) { ans++; a[i+1]=a[i+1]+a[i]-ave; } } printf("%d\n",ans); return 0;}
CodeForces 479B
Tower
Description
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of ai cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2).
The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it‘s a waste of time.
Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task.
Input
The first line contains two space-separated positive integers n and k (1≤n≤100, 1≤k≤1000) — the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers ai (1≤ai≤104) — the towers‘ initial heights.
Output
In the first line print two space-separated non-negative integers s and m (m≤k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that.
In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i≠j). Note that in the process of performing operations the heights of some towers can become equal to zero.
If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.
Sample Input
Input
3 2
5 8 5
Output
0 2
2 1
2 3
Input
3 4
2 2 4
Output
1 1
3 2
Input
5 3
8 3 2 6 3
Output
3 3
1 3
1 2
1 3
Hint In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
题意大概和上面一样、就是每次移动的时候不一定是移动到相邻的、而且每次移动只能移动一个、可以随便移动、则每次从最多的往最少的移动一个就是了。
其实暴力就能过、数据小
#include <iostream>#include <algorithm>#include <cstdio>#include <queue>#include <cmath>#include <map>#include <iterator>#include <cstring>#include <string>using namespace std;#define max(a,b) ((a)>(b)(a):(b))#define min(a,b) ((a)<(b)(a):(b))#define INF 0x7fffffff#define ll long long#define N 1005int n,k;int rx[N],ry[N];multimap<int,int> mp;multimap<int,int>::iterator it1,it2;int main(){ while(scanf("%d%d",&n,&k)!=EOF) { mp.clear(); for(int i=1;i<=n;i++) { int item; scanf("%d",&item); mp.insert(pair<int,int>(item,i)); } int step=1; while(1) { it1=mp.begin(); it2=--mp.end(); if(it2->first-it1->first<=1 || step>k) break;; rx[step]=it2->second; ry[step]=it1->second; mp.insert(pair<int,int>(it1->first+1,it1->second)); mp.insert(pair<int,int>(it2->first-1,it2->second)); mp.erase(it1); mp.erase(it2); step++; } step--; it1=mp.begin(); it2=--mp.end(); int result=(it2->first)-(it1->first); cout<<result<<‘ ‘<<step<<endl; for(int i=1;i<=step;i++) { cout<<rx[i]<<‘ ‘<<ry[i]<<endl; } } return 0;}
------2014-12-14