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. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. #pragma comment(linker, "/STACK:102400000,102400000")
  6. using namespace std;
  7. typedef long long ll;
  8. const int mod = 1000000007;
  9. int a[100050];
  10. int aver,flag,n;
  11. int p[100050][2];
  12. int c[100050];
  13. int tot;
  14.  
  15. bool solve()
  16. {
  17. for(int i = 3; i <= n; i++)
  18. c[i] = a[i];
  19. for(int i = 2; i <= n; i++)
  20. {
  21. if(c[i] == 0)
  22. continue;
  23. else if(c[i] == 1 && i != n)
  24. {
  25. c[i+1]++;
  26. c[i]--;
  27. p[tot][0] = i;
  28. p[tot++][1] = i+1;
  29. }
  30. else if(c[i] == 1 && i == n)
  31. {
  32. c[1]++;
  33. c[i]--;
  34. p[tot][0] = i;
  35. p[tot++][1] = 1;
  36. }
  37. else if(c[i] == -1 && i!= n)
  38. {
  39. c[i]++;
  40. c[i+1]--;
  41. p[tot][0] = i+1;
  42. p[tot++][1] = i;
  43. }
  44. else if(c[i] == -1 && i== n)
  45. {
  46. c[i]++;
  47. c[1]--;
  48. p[tot][0] = 1;
  49. p[tot++][1] = i;
  50. }
  51. else if(c[i] >1 || c[i] < -1)
  52. return false;
  53. }
  54. for(int i = 1; i <= n; i++)
  55. if(c[i]!=0)
  56. return false;
  57. return true;
  58. }
  59.  
  60. void prin()
  61. {
  62. printf("YES\n");
  63. printf("%d\n",tot);
  64. for(int i = 0; i < tot; i++)
  65. printf("%d %d\n",p[i][0],p[i][1]);
  66. }
  67.  
  68. int main()
  69. {
  70. int T;
  71. //freopen("01.txt","r",stdin);
  72. scanf("%d",&T);
  73. while(T--)
  74. {
  75. scanf("%d",&n);
  76. ll sum = 0;
  77. memset(p,0,sizeof(p));
  78. for(int i = 1; i <= n; i++)
  79. {
  80. scanf("%d",&a[i]);
  81. sum += a[i];
  82. }
  83.  
  84. if(sum % n)
  85. {
  86. printf("NO\n");
  87. continue;
  88. }
  89. aver = sum / n;
  90. flag = 0;
  91. tot = 0;
  92. for(int j = 1; j <= n; j++)
  93. a[j] =a[j] - aver;
  94. c[1] = a[1];
  95. c[2] = a[2];
  96. if(solve())
  97. {
  98. prin();
  99. }
  100. else
  101. {
  102. tot = 0;
  103. c[1] = a[1] - 1;
  104. c[2] = a[2] + 1;
  105. p[tot][0] = 1;
  106. p[tot++][1] = 2;
  107. if(solve())
  108. prin();
  109. else
  110. {
  111. tot = 0;
  112. c[1] = a[1] + 1;
  113. c[2] = a[2] - 1;
  114. p[tot][0] = 2;
  115. p[tot++][1] = 1;
  116. if(solve())
  117. prin();
  118. else
  119. printf("NO\n");
  120. }
  121. }
  122.  
  123. }
  124. return 0;
  125. }

  

2015 多校联赛 ——HDU5353(构造)的更多相关文章

  1. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  2. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. 2015 多校联赛 ——HDU5294(最短路,最小切割)

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  4. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  5. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  6. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  8. 2015 多校联赛 ——HDU5301(技巧)

    Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...

  9. 2015 多校联赛 ——HDU5303(贪心)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

随机推荐

  1. django模板(一)

    模板(一) 实验简介 在前一章中,你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. def current_datetime(req ...

  2. logging日志

    import logging logging.basicConfig(filename='log.log', format='%(asctime)s - %(name)s - %(levelname) ...

  3. Django 测试驱动开发

    第一章 1.编写functional_tests.py from selenium import webdriver browser = webdriver.Firefox() browser.get ...

  4. D的下L

    D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2   描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给 ...

  5. bootstrap的ajax提交

    一般后台界面都用bootstrap框架,这是一个css框架,里面封装了ajax方法,只需要在样式中指定就行,根本自己不用写 <td> <eq name='item.status' v ...

  6. php的set_time_limit()函数

    set_time_limit(0); 括号里边的数字是执行时间,如果为零说明永久执行直到程序结束,如果为大于零的数字,则不管程序是否执行完成,到了设定的秒数,程序结束. 一个简单的例子,在网页里显示1 ...

  7. Java.nio-随机读写汉字

    笔者最近在用多线程来计算中文文本的标点符号数目,遇到了以下问题: 在Windows下,文本中汉字通常采用Unicode编码,这就导致需要随机(RandomAccessFile)读取文本时,产生乱码现象 ...

  8. docker实践4

    我的docker学习笔记4-守护式容器   $docker run -i -t ubuntu /bin/bash $ctrl-p 或 ctrl-q # 转到后台   $docker ps $docke ...

  9. gradle入门(1-7)eclipse和gradle集成插件的安装和使用

    一.安装gradle插件:buildship 1.安装插件 gradle默认的本地缓存库在c盘user目录下的.gradle文件夹下,安装好gradle后,可以添加环境变量GRADLE_USER_HO ...

  10. SpringCloud的微服务网关:zuul(实践)

    Zuul的主要功能是路由和过滤器.路由功能是微服务的一部分,比如/api/user映射到user服务,/api/shop映射到shop服务.zuul实现了负载均衡. zuul有以下功能: Authen ...