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. xml 充当简易数据库

    后台: 写入节点 public static void Update(string path, string node, string attribute, string value) { try { ...

  2. 关于对象和this、new

    //创建一个猫类 function Cat(name,color,eyeColor){ //上面处Cat首字母大写表示创建一个‘类别’叫Cat类.假如首字母小写cat则是创建一个cat的函数: thi ...

  3. activity的横屏和竖屏设置

    主要在清单文件这样配置: <application android:allowBackup="true" android:icon="@drawable/ic_la ...

  4. 二分查找(Java)

    二分查找的前提的要查找的数组必须有序. 代码如下: 程序1 public class source { public int binary_sort(int[] array, int item) { ...

  5. pcap文件格式解析

    pcap文件格式是常用的数据报存储格式,包括wireshark在内的主流抓包软件都可以生成这种格式的数据包 下面对这种格式的文件简单分析一下:    pcap文件的格式为:  文件头    24字节  ...

  6. objectiveC获取本地时间。

    NSDate * date = [NSCalendarDate date]; NSLog(@"%@", date); 日历在IOS里报错,,. NSDateFormatter *f ...

  7. .net mvc Authorization Filter,Exception Filter与Action Filter

    一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...

  8. 盘点:移动服务 #AzureChat

    感谢大家帮助我们顺利推出史无前例的 #AzureChat.移动服务和 Notification Hub 是 Windows Azure 平台上令人振奋的服务.我们很高兴能借这次在线讨论的机会,倾听各位 ...

  9. QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NETQT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET

    QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET QT在ui文件上建立信号操机制会不会对后期维护产生影响 分类: 学习心得 2 ...

  10. p2.js物理引擎学习

    P2简介 P2是一款基于Javascript编写的HTML5 2D物理引擎,和Box2D.Nape等2D物理引擎一样,P2集成了各种复杂的物理公式和算法,可以帮助我们轻松的实现碰撞.反弹等物理现象的模 ...