码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5353 Average(模拟)

时间:2015-08-07 23:34:15      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

Average

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1457    Accepted Submission(s): 360
Special Judge


Problem Description
There are n soda sitting around a round table. soda are numbered from 1 to n and i-th soda is adjacent to (i+1)-th soda, 1-st soda is adjacent to n-th soda.

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.

Now you are to determine whether it is possible and give a sequence of operations.
 


Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains an integer n (1n105), the number of soda.
The next line contains n integers a1,a2,,an (0ai109), where ai denotes the candy i-th soda has.
 


Output
For each test case, output "YES" (without the quotes) if possible, otherwise output "NO" (without the quotes) in the first line. If possible, then the output an integer m (0mn) in the second line denoting the number of operations needed. Then each of the following m lines contain two integers x and y (1x,yn), which means that x-th soda gives y-th soda a candy.
 


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

不能被整除,以及与平均值的差值超过2的一定是不可行的。然后就是把所有需要操作的点提出来,把差值为2的分成两个1就行,然后找到两个连续为1或者-1的,然后开始往右边扫

  1 /**
  2  * code generated by JHelper
  3  * More info: https://github.com/AlexeyDmitriev/JHelper
  4  * @author xyiyy @https://github.com/xyiyy
  5  */
  6 
  7 #include <iostream>
  8 #include <fstream>
  9 
 10 //
 11 // Created by xyiyy on 2015/8/7.
 12 //
 13 
 14 #ifndef JHELPER_EXAMPLE_PROJECT_LIBG_HPP
 15 #define JHELPER_EXAMPLE_PROJECT_LIBG_HPP
 16 
 17 #include <bits/stdc++.h>
 18 #include <ext/hash_map>
 19 #include <ext/hash_set>
 20 #include <ext/pb_ds/assoc_container.hpp>
 21 #include <ext/pb_ds/tree_policy.hpp>
 22 #include <ext/pb_ds/priority_queue.hpp>
 23 
 24 using namespace std;
 25 using namespace __gnu_cxx;
 26 using namespace __gnu_pbds;
 27 #define mp(X, Y) make_pair(X,Y)
 28 #define pb(X) push_back(X)
 29 #define rep(X, N) for(int X=0;X<N;X++)
 30 typedef long long ll;
 31 typedef pair<int, int> PII;
 32 typedef vector<PII> VII;
 33 #endif //JHELPER_EXAMPLE_PROJECT_LIBG_HPP
 34 
 35 #define gao() out<<"NO"<<endl
 36 int a[100010];
 37 
 38 class hdu5353 {
 39 public:
 40     void solve(std::istream &in, std::ostream &out) {
 41         int n;
 42         in >> n;
 43         rep(i, n)in >> a[i];
 44         ll tot = 0;
 45         rep(i, n)tot += a[i];
 46         if (tot % n != 0) {
 47             gao();
 48             return;
 49         }
 50         int ok = 1;
 51         int ave = tot / n;
 52         VII v;
 53         rep(i, n) {
 54             a[i] -= ave;
 55             if (a[i] < -2 || a[i] > 2)ok = 0;
 56             else if (a[i] == -1 || a[i] == 1)v.pb(mp(a[i], i));
 57             else if (a[i] == -2 || a[i] == 2)v.pb(mp(a[i] / 2, i)), v.pb(mp(a[i] / 2, i));
 58         }
 59         if (!ok) {
 60             gao();
 61             return;
 62         }
 63         int sz = v.size();
 64         if (sz & 1) {
 65             gao();
 66             return;
 67         }
 68         int st = 0;
 69         rep(i, sz) {
 70             if (v[(i + sz - 1) % sz].first == v[i].first)st = i;
 71         }
 72         int e = st;
 73         VII ans;
 74         if (sz) {
 75             while (1) {
 76                 int a = v[st].first, b = v[(st + 1) % sz].first;
 77                 int l = v[st].second, r = v[(st + 1) % sz].second;
 78                 if (a == b) {
 79                     ok = 0;
 80                     break;
 81                 } else if (a == 1) {
 82                     for (; l != r; (l += 1) %= n)ans.pb(mp(l, (l + 1) % n));
 83                 } else {
 84                     for (; r != l; (r += n - 1) %= n)ans.pb(mp(r, (r + n - 1) % n));
 85                 }
 86                 (st += 2) %= sz;
 87                 if (st == e)break;
 88             }
 89         }
 90         if (!ok) {
 91             gao();
 92             return;
 93         }
 94         out << "YES" << endl << ans.size() << endl;
 95         rep(i, ans.size()) {
 96             out << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
 97         }
 98     }
 99 };
100 
101 int main() {
102     std::ios::sync_with_stdio(false);
103     std::cin.tie(0);
104     hdu5353 solver;
105     std::istream &in(std::cin);
106     std::ostream &out(std::cout);
107     int n;
108     in >> n;
109     for (int i = 0; i < n; ++i) {
110         solver.solve(in, out);
111     }
112 
113     return 0;
114 }

 

hdu5353 Average(模拟)

标签:

原文地址:http://www.cnblogs.com/fraud/p/4712138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!