转载请注明出处: 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 (1≤n≤105), the number of soda.
The next line contains n integers a1,a2,…,an (0≤ai≤109), 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 (0≤m≤n) in the second line denoting the number of operations needed. Then each of the following m lines contain two integers x and y (1≤x,y≤n), 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的,然后开始往右边扫

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //
// Created by xyiyy on 2015/8/7.
// #ifndef JHELPER_EXAMPLE_PROJECT_LIBG_HPP
#define JHELPER_EXAMPLE_PROJECT_LIBG_HPP #include <bits/stdc++.h>
#include <ext/hash_map>
#include <ext/hash_set>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp> using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define mp(X, Y) make_pair(X,Y)
#define pb(X) push_back(X)
#define rep(X, N) for(int X=0;X<N;X++)
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<PII> VII;
#endif //JHELPER_EXAMPLE_PROJECT_LIBG_HPP #define gao() out<<"NO"<<endl
int a[]; class hdu5353 {
public:
void solve(std::istream &in, std::ostream &out) {
int n;
in >> n;
rep(i, n)in >> a[i];
ll tot = ;
rep(i, n)tot += a[i];
if (tot % n != ) {
gao();
return;
}
int ok = ;
int ave = tot / n;
VII v;
rep(i, n) {
a[i] -= ave;
if (a[i] < - || a[i] > )ok = ;
else if (a[i] == - || a[i] == )v.pb(mp(a[i], i));
else if (a[i] == - || a[i] == )v.pb(mp(a[i] / , i)), v.pb(mp(a[i] / , i));
}
if (!ok) {
gao();
return;
}
int sz = v.size();
if (sz & ) {
gao();
return;
}
int st = ;
rep(i, sz) {
if (v[(i + sz - ) % sz].first == v[i].first)st = i;
}
int e = st;
VII ans;
if (sz) {
while () {
int a = v[st].first, b = v[(st + ) % sz].first;
int l = v[st].second, r = v[(st + ) % sz].second;
if (a == b) {
ok = ;
break;
} else if (a == ) {
for (; l != r; (l += ) %= n)ans.pb(mp(l, (l + ) % n));
} else {
for (; r != l; (r += n - ) %= n)ans.pb(mp(r, (r + n - ) % n));
}
(st += ) %= sz;
if (st == e)break;
}
}
if (!ok) {
gao();
return;
}
out << "YES" << endl << ans.size() << endl;
rep(i, ans.size()) {
out << ans[i].first + << " " << ans[i].second + << endl;
}
}
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
hdu5353 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = ; i < n; ++i) {
solver.solve(in, out);
} return ;
}
04-21 04:26