Average
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2153 Accepted Submission(s): 532
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.
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 \le n \le 10^5)$, the number of soda.
The next line contains $n$ integers $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^9)$, where $a_i$ denotes the candy $i$-th soda has.
The first contains an integer $n$ $(1 \le n \le 10^5)$, the number of soda.
The next line contains $n$ integers $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^9)$, where $a_i$ 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 \le m \le 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 \le x, y \le 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
Source
Recommend
wange2014
首先一个人有三种操作,每个人选择的操作影响周边2人.
如果无环的化可直接贪心。
所以枚举人1和人2的关系,把环从1与2之间断开
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define mp make_pair
#define MAXN (100000+10)
typedef long long ll;
int n;
int a[MAXN],a2[MAXN];
void rel(int &a,int &b,int c)
{
a=c-1,b=c+1;
if (a==0) a=n;if (b>n) b=1;
}
pair<int,int>ans[MAXN];
int siz=0;
bool check(int a[])
{
a[n+1]=a[1];
Fork(i,2,n)
{
if (a[i]==-1) a[i]++,a[i+1]--,ans[siz++]=mp(i+1<=n?i+1:1,i);
else if (a[i]==1) a[i]--,a[i+1]++,ans[siz++]=mp(i,i+1<=n?i+1:1);
}
a[1]=a[n+1];
For(i,n) if (a[i]) return 0;
return 1;
}
void pri()
{
cout<<"YES\n"<<siz<<'\n';
Rep(i,siz) printf("%d %d\n",ans[i].first,ans[i].second);
}
int main()
{
// freopen("A.in","r",stdin);
int T;
cin>>T;
For(kcase , T)
{
cin>>n;
For(i,n) scanf("%d",&a[i]);
ll sum=0;
For(i,n) sum+=a[i];
if (sum%n){
cout<<"NO\n";continue;
}
sum/=n;
For(i,n) a[i]-=sum;
bool flag=0;
For(i,n) if (a[i]<-2||a[i]>2){
cout<<"NO\n"; flag=1;break;
}
if (flag==1) continue;
memcpy(a2,a,sizeof(a));
MEM(ans) siz=0;
if (check(a2))
{
pri();
continue;
}
memcpy(a2,a,sizeof(a));
MEM(ans) siz=0;
a2[1]--;a2[2]++;ans[siz++]=mp(1,2);
if (check(a2))
{
pri();
continue;
}
memcpy(a2,a,sizeof(a));
MEM(ans) siz=0;
a2[1]++;a2[2]--;ans[siz++]=mp(2,1);
if (check(a2))
{
pri();
continue;
}
cout<<"NO\n";
}
return 0;
}