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. NHibernate初入门之映射文件配置说明(三)

    转载逆心http://www.cnblogs.com/kissdodog/archive/2013/02/21/2919886.html 1. hibernate-mapping 这个元素包括以下可选 ...

  2. SMACSS:一个关于CSS的最佳实践-1.Overview

    什么是SMACSS? SMACSS(发音"smacks"),全称Scalable and Modular Architecture for CSS.顾名思义,SMACSS是一个可扩 ...

  3. javascript 的工具方法 --- 类型判断

    Javascript中常见类型对象有: Boolean, Number, String, Function, Array, Date, RegExp, Object, Error, Symbol等等. ...

  4. iOS 7 标签栏控制器进行模态视图跳转后变成透明

    要解决此问题,需要设置tabBar的如下属性: self.tabBar.translucent = NO;

  5. JS图表组件 highcharts 简单的介绍

    把highcharts拿来做个简单的介绍,希望更多的朋友可以用到这个用来做图表的js插件. preparation Highcharts Highcharts是一个制作图表的纯Javascript类库 ...

  6. Android应用开发基础篇(7)-----BroadcastReceiver

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/22/2363644.html 一.概述 BroadcastReceiver,意思就是广播信息接收 ...

  7. JavaSE学习总结第27天_反射 & 设计模式 & JDK5、7、8新特性

      27.01  反射_类的加载概述和加载时机 类的加载:当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载:就是指将class文件读 ...

  8. Android UiAutomator 自动化测试一些代码实例---新手3

    1.打开浏览器,打开百度实例 public void testBrowser() throws RemoteException, UiObjectNotFoundException{ //灭屏幕-亮屏 ...

  9. Array.Add () and += in PowerShell

    $newArray = @() $newArray.Add("Hello") If I create a new array, and using the method Add() ...

  10. HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误 #Reprinted#

    请求方法是请求一定的Web页面的程序或用于特定的URL. 可选用下列几种: GET: 请求指定的页面信息,并返回实体主体. HEAD: 只请求页面的首部. POST: 请求服务器接受所指定的文档作为对 ...