Average

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2756    Accepted Submission(s): 650 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
**********
* Author :Running_Time
* Created Time :2015-8-7 8:51:48
* File Name :A.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
int a[MAXN];
int x[MAXN];
int n, tot;
ll ave; bool work(void){
tot = ;
if (x[] != ) tot++;
if (x[n] != ) tot++;
if (x[n-] != ) tot++;
for (int i=; i<=n-; ++i) {
if (abs (a[i] - ave + x[i-]) > ) return false;
x[i] = a[i] - ave + x[i-];
if (x[i] != ) tot++;
}
if (a[n-] - x[n-] + x[n-] != ave) return false;
return true;
} bool judge(void){
for (int i=-; i<=; ++i) {
for (int j=-; j<=; ++j) {
x[] = i, x[n] = j;
if (abs (ave - a[n] + x[n]) <= ) {
x[n-] = ave - a[n] + x[n];
if (work ()) return true;
}
}
}
return false;
} int main(void) { //HDOJ 5353 Average
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n); ll sum = ;
/*
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]); sum += a[i];
}
if (sum % n != 0) {
puts ("NO"); continue;
}
ave = sum / n; bool flag = true, same = true;
for (int i=1; i<=n; ++i) {
if (a[i] < ave - 2 || a[i] > ave + 2){
flag = false; break;
}
if (a[i] != ave) same = false;
}
if (!flag) {
puts ("NO"); continue;
}
if (same) {
printf ("YES\n0\n"); continue;
}
*/
memset (x, , sizeof (x));
if (!judge ()) {
puts ("NO"); continue;
}
puts ("YES"); printf ("%d\n", tot);
for (int i=; i<=n; ++i) {
if (x[i] == ) continue;
else if (x[i] == ) printf ("%d %d\n", i, (i == n) ? : i + );
else printf ("%d %d\n", (i == n) ? : i + , i);
}
} return ;
}

题解:神奇的模拟,看了两天;x数组保存对于下一个值的操作;参考大神的写的,还是错。。。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+;
int tot,n;
int x[MAXN],a[MAXN];
typedef long long LL;
LL ave;
bool work(){
tot=;
if(x[]!=)tot++;
if(x[n]!=)tot++;
if(x[n-]!=)tot++;
for(int i=;i<=n-;i++){
if(abs(a[i]-ave+x[i-])>)return false;
x[i]=a[i]-ave+x[i-];
if(x[i]!=)tot++;
}
if(a[n-]-x[n-]+x[n-]!=ave)return false;
return true;
}
bool find(){
for(int i=-;i<=;i++){
for(int j=-;j<=;j++){
x[]=i;x[n]=j;
if(abs(ave-a[n]+x[n])<=){
x[n-]=ave-a[n]+x[n];
if(work())return true;
}
}
}
return false;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(x,,sizeof(x));
LL sum=;
for(int i=;i<=n;i++)scanf("%d",a+i),sum+=a[i];
if(sum%n!=){
puts("NO");continue;
}
ave=sum/n;
int flot=,same=;
for(int i=;i<=n;i++){
if(abs(a[i]-ave)>=){
puts("NO");flot=;break;
}
if(a[i]!=same)same=;
}
if(flot)continue;
if(same){
puts("YES\n0");continue;
}
if(find()){
puts("YES");
}
else{
puts("NO");continue;
}
printf("%d\n",tot);
// for(int i=1;i<=n;i++)printf("%d ",x[i]);puts("");
for(int i=;i<=n;i++){
if(!x[i])continue;
if(x[i]==)printf("%d %d\n",i,i==n?:i+);
else printf("%d %d\n",i==n?:i+,i);
}
}
return ;
}

Average(模拟)的更多相关文章

  1. hdu5353 Average(模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Average Time Limit: 4000/2000 MS (Java/Ot ...

  2. STL复习之 map & vector --- disney HDU 2142

    题目链接: https://vjudge.net/problem/40913/origin 大致题意: 这是一道纯模拟题,不多说了. 思路: map模拟,vector辅助 其中用了map的函数: er ...

  3. training 2

    Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.136 Average Precision (AP) @[ IoU ...

  4. HDU 5353 Average 糖果分配(模拟,图)

    题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路: 明显每个人最多只能多于平均值2个 ...

  5. Lucky and Good Months by Gregorian Calendar - POJ3393模拟

    Lucky and Good Months by Gregorian Calendar Time Limit: 1000MS Memory Limit: 65536K Description Have ...

  6. HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)

    Relative atomic mass Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. 理解Load Average做好压力测试

    http://www.blogjava.net/cenwenchu/archive/2008/06/30/211712.html CPU时间片 为了提高程序执行效率,大家在很多应用中都采用了多线程模式 ...

  8. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  9. [进程管理] Linux中Load average的理解

    Load average的定义 系统平均负载被定义为在特定时间间隔内运行队列中的平均进程树.如果一个进程满足以下条件则其就会位于运行队列中: - 它没有在等待I/O操作的结果 - 它没有主动进入等待状 ...

随机推荐

  1. JS年月日三级联动下拉框日期选择代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 利用 squid 反向代理提高网站性能

    http://www.ibm.com/developerworks/cn/linux/l-cn-squid/ http://www.squid-cache.org/ http://www.beijin ...

  3. eclipse:Tomcat设置jvm,解决java.lang.OutOfMemoryError: Java heap space 堆内存溢出

    eclipse 有启动参数里设置jvm大小,因为eclipse运行时自己也需要jvm,所以eclipse.ini里设置的jvm大小不是具体某个程序运行时所用jvm的大小,这和具体程序运行的jvm大小无 ...

  4. 马踏棋盘问题-贪心(MATLAB&C++)

    原创文章,转载请注明:马踏棋盘问题-贪心(MATLAB&C++) By Lucio.Yang 1.问题描述 将马随机放在国际象棋的Board[0-7][0-7]的某个方格中,马按走棋规则进行移 ...

  5. 帝国cms分页样式修改文件-注意事项

    帝国cms分页样式主要有:内容页分页样式.列表页分页样式以及默认搜索模板使用的搜索样式等几种. 要改这些样式其实也很简单,在网站目录中找到相应的.css文件修改empages属性就行了,但是这样比较麻 ...

  6. codeforces 417D. Cunning Gena 状压dp

    题目链接 D. Cunning Gena time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. 文件上传 PHP

    参考http://www.w3school.com.cn/php/php_file_upload.asp 文件上传实际上是一个文件复制的过程  当我们选中一个文件之后  php默认的tmp文件夹中就有 ...

  8. 将dll放进exe[.Net]

    原文:将dll放进exe[.Net] 两种方案: 1.使用ILMerge工具. 缺点:需离开工程,使用第三方工具(ILMerge). 2.将dll作为Resource放进exe,exe执行时动态加载( ...

  9. sed(查找替换) 与awk(提取字段)

    通常: sed 处理列  awk处理行 比较方便 Sed是一个基本的查找替换程序     sed -i   "s/^@//g"     文件  #原地操作原文件,进行替换 cat ...

  10. Ubuntu Linux: How Do I install .deb Packages?

    Ubuntu Linux: How Do I install .deb Packages? Ubuntu Linux: How Do I install .deb Packages? by Nix C ...