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. 2.Freshman阶段学习内容的确定

    我刷知乎.在知乎上答题的程序员,不是很牛逼就是更牛逼,说起各种系统.各种系统的各种版本.各种语言.数据库.算法.IT届的各种圣战都有板有眼.信手拈来.头头是道,不得不服.这导致了一些非常严重的问题:我 ...

  2. C#中两个整数相除得到带小数点的结果

    有时候需要将两个整数相除,获得带小数点的float类型数.例如一个整数12345,需要变成123.45.常见与串口与硬件通讯,DSP处理浮点型比较麻烦,DSP传递来的温度等数据都以整型的方式传递,串口 ...

  3. BAE初试

    BAE是百度的应用开发托管平台.  支持python nodejs java php 这几个环境~ 我在BAE上面搭建了1个wordpress. 记录下开启一个app的过程. 下面是所需工具 ---版 ...

  4. 关于MVC EntityType has no key defined的问题

    关于MVC EntityType has no key defined的问题 在测试MVC时发现一个奇怪的问题,在Model中定义数据类时,如 public class UserInfo { publ ...

  5. HTML+CSS笔记 CSS入门续集

    继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代(标签). 语法: p{color:red;} <p> ...

  6. Git Command Summary (Updated)

    取得Git仓库 初始化一个版本仓库 git init Clone远程版本库 git clone git@xbc.me:wordpress.git 添加远程版本库origin,语法为 git remot ...

  7. IOS 学习笔记(4) 控件 标签(UILabel)的使用方法

    虽说Label的中文翻译是标签标记,但它其实是一个静态文本内容的展现控件. 一般来说,UILabel只是一个只读的文本视图,开发者可以利用UiLabel来展示内容长度有固定上限的文字内容.并且,UIL ...

  8. 转: vim简明教程

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  9. java.lang.VerifyError: Inconsistent stackmap frames at branch target

    -XX:-UseSplitVerifier解决. 附带网址:http://stackoverflow.com/questions/12774672/java-7-inconsistent-stackm ...

  10. Qt之美(一):d指针/p指针详解

    Translated  by  mznewfacer   2011.11.16 首先,看了Xizhi Zhu 的这篇Qt之美(一):D指针/私有实现,对于很多批评不美的同路人,暂且不去评论,只是想支持 ...