转载请注明出处: 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
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.
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.
不能被整除,以及与平均值的差值超过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 ;
}