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. 【转】sqlserver数据库之间的表的复制

    以下以数据库t1和test为例.  1.复制表结构及资料 select * into 数据库名.dbo.表名 from 源表(全部数据)     如:select * into t1.dbo.YS1 ...

  2. POJ 1797 Heavy Transportation 最短路变形(dijkstra算法)

    题目:click here 题意: 有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量.分析: 其实这个求最大边可以 ...

  3. BZOJ 2329: [HNOI2011]括号修复( splay )

    把括号序列后一定是))))((((这种形式的..所以维护一个最大前缀和l, 最大后缀和r就可以了..答案就是(l+1)/2+(r+1)/2...用splay维护,O(NlogN). 其实还是挺好写的, ...

  4. libuv 错误号UV_ECANCELED 的处理

    这个地方有好多的不明白,也没仔细的看懂代码,希望有牛人指点. 先记录几个issue 1. after_write recv many(>10000) ECANCELED in my write_ ...

  5. load和ready

    <一>ready和load ready先执行,load后执行 DOM文档加载的步骤: () 解析HTML结构. () 加载外部脚本和样式表文件. () 解析并执行脚本代码. () 构造HT ...

  6. Lucence.Net+添加关键词+分页+排序

    1.使用queryparser完成解析搜索请求 2.基本格式如: QueryParser parser=new QueryParser("字段名称","分析器实例&quo ...

  7. 简单的UIScrollView 下拉刷新

    这里只贴主要代码 #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate&g ...

  8. [LeetCode]题解(python):132-Palindrome Partitioning II

    题目来源: https://leetcode.com/problems/palindrome-partitioning-ii/ 题意分析: 给定一个s,可以将s拆成若干个回文子字符串之和,如果拆成了m ...

  9. include file和include virtual的区别

    1.#include file 包含文件的相对路径,#include virtual包含文件的虚拟路径. 2.在同一个虚拟目录内,<!--#include file="file.asp ...

  10. Dubbo原理解析-监控

    Dubbo发布代码中,自带了一个简易的监控中心实现.对于一般的小业务这个监控中心应该能够满足需求,对于那些大业务量的大公司一般都会有自己的监控中心,更加丰富的功能如常用的报警短信通知等等.这章讲解分析 ...