转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Average

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1457    Accepted Submission(s): 360
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

不能被整除,以及与平均值的差值超过2的一定是不可行的。然后就是把所有需要操作的点提出来,把差值为2的分成两个1就行,然后找到两个连续为1或者-1的,然后开始往右边扫

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //
// Created by xyiyy on 2015/8/7.
// #ifndef JHELPER_EXAMPLE_PROJECT_LIBG_HPP
#define JHELPER_EXAMPLE_PROJECT_LIBG_HPP #include <bits/stdc++.h>
#include <ext/hash_map>
#include <ext/hash_set>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp> using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define mp(X, Y) make_pair(X,Y)
#define pb(X) push_back(X)
#define rep(X, N) for(int X=0;X<N;X++)
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<PII> VII;
#endif //JHELPER_EXAMPLE_PROJECT_LIBG_HPP #define gao() out<<"NO"<<endl
int a[]; class hdu5353 {
public:
void solve(std::istream &in, std::ostream &out) {
int n;
in >> n;
rep(i, n)in >> a[i];
ll tot = ;
rep(i, n)tot += a[i];
if (tot % n != ) {
gao();
return;
}
int ok = ;
int ave = tot / n;
VII v;
rep(i, n) {
a[i] -= ave;
if (a[i] < - || a[i] > )ok = ;
else if (a[i] == - || a[i] == )v.pb(mp(a[i], i));
else if (a[i] == - || a[i] == )v.pb(mp(a[i] / , i)), v.pb(mp(a[i] / , i));
}
if (!ok) {
gao();
return;
}
int sz = v.size();
if (sz & ) {
gao();
return;
}
int st = ;
rep(i, sz) {
if (v[(i + sz - ) % sz].first == v[i].first)st = i;
}
int e = st;
VII ans;
if (sz) {
while () {
int a = v[st].first, b = v[(st + ) % sz].first;
int l = v[st].second, r = v[(st + ) % sz].second;
if (a == b) {
ok = ;
break;
} else if (a == ) {
for (; l != r; (l += ) %= n)ans.pb(mp(l, (l + ) % n));
} else {
for (; r != l; (r += n - ) %= n)ans.pb(mp(r, (r + n - ) % n));
}
(st += ) %= sz;
if (st == e)break;
}
}
if (!ok) {
gao();
return;
}
out << "YES" << endl << ans.size() << endl;
rep(i, ans.size()) {
out << ans[i].first + << " " << ans[i].second + << endl;
}
}
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
hdu5353 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = ; i < n; ++i) {
solver.solve(in, out);
} return ;
}

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

  1. hdu5353 Average

    Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...

  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. Average(模拟)

      Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

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

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

  6. [hdu5353]模拟

    题意:有n个小朋友,每个小朋友手上有一些糖,考虑每两个相邻的小朋友a.b,可以选择执行3种操作中的任一种:(1)a给b一粒糖(2)b给a一粒糖(3)不进行任何动作,问能否通过确定每两个相邻的小朋友的操 ...

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

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

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

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

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

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

随机推荐

  1. POJ 3630 , HDU 1671 Phone List - from lanshui_Yang

    这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...

  2. 恢复root用户目录,及~目录

    普通帐号登su;mkdir /root;chown root:root /root cp -R /etc/skel/.[!.]* ./

  3. 使用NSTimer实现倒计时-备

    今天在CocoaChina上面看到有人在问倒计时怎么做,记得以前在看Iphone31天的时候做过一个,今天翻出来运行不了了,原因是我的IphoneSDK升级到3.1了,以前使用的是2.2.1,在2.2 ...

  4. ububtu 彻底卸载程序的几种方法

    sudo apt-get purge ......(点点为为程序名称) sudo apt-get autoremove sudo apt-get clean dpkg -l |grep ^rc|awk ...

  5. JUnit使用Eclipse建立Test Case - 就是爱Java

    传统的测试方式,是以main(),作为代码的起点,而这次Mix将利用JUnit 进行测试,Eclipse有提供完整的整合环境,可以方便地使用JUnit,让Mix快速地进行单元测试,首先,开始撰写第1个 ...

  6. Delphi 函数指针(函数可以当参数)

    首先学习: 指向非对象(一般的)函数/过程的函数指针 Pascal 中的过程类型与C语言中的函数指针相似,为了统一说法,以下称函数指针.函数指针的声明只需要参数列表:如果是函数,再加个返回值.例如声明 ...

  7. Qt C++中的关键字explicit——防止隐式转换(也就是Java里的装箱),必须写清楚

    最近在复习QT,准备做项目了,QT Creator 默认生成的代码 explicit Dialog(QWidget *parent = 0)中,有这么一个关键字explicit,用来修饰构造函数.以前 ...

  8. Linux企业级项目实践之网络爬虫(2)——网络爬虫的结构与工作流程

    网络爬虫是捜索引擎抓取系统的重要组成部分.爬虫的主要目的是将互联网上的网页下载到本地形成一个或联网内容的镜像备份. 一个通用的网络爬虫的框架如图所示:

  9. cf493C Vasya and Basketball

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  10. MyEclipse第一个Servlet程序 --解决Win7系统下MyEclipse与Tomcat连接问题

    前言 本文旨在帮助学习java web开发的人员,熟悉环境,在Win7系统下运行自己的第一个Servlet程序,因为有时候配置不当或系统原因可能会运行不成功,这给初学者带来了一定烦恼,我也是为此烦恼过 ...