2015 多校联赛 ——HDU5353(构造)
2015-08-06 19:42
253 查看
Each soda has some candies in their hand. And they want to make the number of candies the same by doing some taking and giving operations. More specifically, every two adjacent soda x and y can do one of the following operations only once:
1. x-th
soda gives y-th
soda a candy if he has one;
2. y-th
soda gives x-th
soda a candy if he has one;
3. they just do nothing.
Sample Input
3
6
1 0 1 0 0 0
5
1 1 1 1 1
3
1 2 3
Sample Output
NO
YES
0
YES
2
2 1
3 2
对相邻两个数之间进行以下三种操作的一种,最后使他们相等
①a++ b-- ②a-- b++ ③nothing
如果(sum%n != 0),直接失败。用一个数组来记录数与平均数之间的差值。
先枚举第一位数的三种情况,
当C[i] == 1时,从C[i+1]取一;
当C[i] == -1时,给C[i+1]一个;
当C[i] == 0时,nothing;
else:false。
ps:完全没想到要对第一位进行枚举 OoO,一直wa
1. x-th
soda gives y-th
soda a candy if he has one;
2. y-th
soda gives x-th
soda a candy if he has one;
3. they just do nothing.
Sample Input
3
6
1 0 1 0 0 0
5
1 1 1 1 1
3
1 2 3
Sample Output
NO
YES
0
YES
2
2 1
3 2
对相邻两个数之间进行以下三种操作的一种,最后使他们相等
①a++ b-- ②a-- b++ ③nothing
如果(sum%n != 0),直接失败。用一个数组来记录数与平均数之间的差值。
先枚举第一位数的三种情况,
当C[i] == 1时,从C[i+1]取一;
当C[i] == -1时,给C[i+1]一个;
当C[i] == 0时,nothing;
else:false。
ps:完全没想到要对第一位进行枚举 OoO,一直wa
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; typedef long long ll; const int mod = 1000000007; int a[100050]; int aver,flag,n; int p[100050][2]; int c[100050]; int tot; bool solve() { for(int i = 3; i <= n; i++) c[i] = a[i]; for(int i = 2; i <= n; i++) { if(c[i] == 0) continue; else if(c[i] == 1 && i != n) { c[i+1]++; c[i]--; p[tot][0] = i; p[tot++][1] = i+1; } else if(c[i] == 1 && i == n) { c[1]++; c[i]--; p[tot][0] = i; p[tot++][1] = 1; } else if(c[i] == -1 && i!= n) { c[i]++; c[i+1]--; p[tot][0] = i+1; p[tot++][1] = i; } else if(c[i] == -1 && i== n) { c[i]++; c[1]--; p[tot][0] = 1; p[tot++][1] = i; } else if(c[i] >1 || c[i] < -1) return false; } for(int i = 1; i <= n; i++) if(c[i]!=0) return false; return true; } void prin() { printf("YES\n"); printf("%d\n",tot); for(int i = 0; i < tot; i++) printf("%d %d\n",p[i][0],p[i][1]); } int main() { int T; //freopen("01.txt","r",stdin); scanf("%d",&T); while(T--) { scanf("%d",&n); ll sum = 0; memset(p,0,sizeof(p)); for(int i = 1; i <= n; i++) { scanf("%d",&a[i]); sum += a[i]; } if(sum % n) { printf("NO\n"); continue; } aver = sum / n; flag = 0; tot = 0; for(int j = 1; j <= n; j++) a[j] =a[j] - aver; c[1] = a[1]; c[2] = a[2]; if(solve()) { prin(); } else { tot = 0; c[1] = a[1] - 1; c[2] = a[2] + 1; p[tot][0] = 1; p[tot++][1] = 2; if(solve()) prin(); else { tot = 0; c[1] = a[1] + 1; c[2] = a[2] - 1; p[tot][0] = 2; p[tot++][1] = 1; if(solve()) prin(); else printf("NO\n"); } } } return 0; }
相关文章推荐
- hibernate SQL Error: 8152, SQLState: 22001
- 反转一个单向链表
- poj-3687 Labeling Balls
- 2015 多校联赛 ——HDU5353(构造)
- Lua笔记7-闭包2
- nyoj 67 三角形面积【三角形面积公式】
- Linux平台日常运维管理
- Java之String类
- Shell编程基础
- UVa 558 Wormholes (判负圈+Bellman-Ford算法)
- Prim
- Hibernate框架之HQL查询与Criteria 查询的区别
- 游戏引擎网络开发者的64做与不做(二A):协议与API
- 【机房合作】重新认识外观模式
- lofter~~
- [POJ 1947] Rebuilding Roads 树形dp
- C/C++中sscanf && sprintf()
- UDP即时小通信
- scala实现单例模式
- 当心从10亿估值沦落到一无所获!创业者签订融资协议时该注意什么?