hdu5353 Average(模拟)

转载请注明出处: 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的,然后开始往右边扫

  1 /**
  2  * code generated by JHelper
  3  * More info: https://github.com/AlexeyDmitriev/JHelper
  4  * @author xyiyy @https://github.com/xyiyy
  5  */
  6
  7 #include <iostream>
  8 #include <fstream>
  9
 10 //
 11 // Created by xyiyy on 2015/8/7.
 12 //
 13
 14 #ifndef JHELPER_EXAMPLE_PROJECT_LIBG_HPP
 15 #define JHELPER_EXAMPLE_PROJECT_LIBG_HPP
 16
 17 #include <bits/stdc++.h>
 18 #include <ext/hash_map>
 19 #include <ext/hash_set>
 20 #include <ext/pb_ds/assoc_container.hpp>
 21 #include <ext/pb_ds/tree_policy.hpp>
 22 #include <ext/pb_ds/priority_queue.hpp>
 23
 24 using namespace std;
 25 using namespace __gnu_cxx;
 26 using namespace __gnu_pbds;
 27 #define mp(X, Y) make_pair(X,Y)
 28 #define pb(X) push_back(X)
 29 #define rep(X, N) for(int X=0;X<N;X++)
 30 typedef long long ll;
 31 typedef pair<int, int> PII;
 32 typedef vector<PII> VII;
 33 #endif //JHELPER_EXAMPLE_PROJECT_LIBG_HPP
 34
 35 #define gao() out<<"NO"<<endl
 36 int a[100010];
 37
 38 class hdu5353 {
 39 public:
 40     void solve(std::istream &in, std::ostream &out) {
 41         int n;
 42         in >> n;
 43         rep(i, n)in >> a[i];
 44         ll tot = 0;
 45         rep(i, n)tot += a[i];
 46         if (tot % n != 0) {
 47             gao();
 48             return;
 49         }
 50         int ok = 1;
 51         int ave = tot / n;
 52         VII v;
 53         rep(i, n) {
 54             a[i] -= ave;
 55             if (a[i] < -2 || a[i] > 2)ok = 0;
 56             else if (a[i] == -1 || a[i] == 1)v.pb(mp(a[i], i));
 57             else if (a[i] == -2 || a[i] == 2)v.pb(mp(a[i] / 2, i)), v.pb(mp(a[i] / 2, i));
 58         }
 59         if (!ok) {
 60             gao();
 61             return;
 62         }
 63         int sz = v.size();
 64         if (sz & 1) {
 65             gao();
 66             return;
 67         }
 68         int st = 0;
 69         rep(i, sz) {
 70             if (v[(i + sz - 1) % sz].first == v[i].first)st = i;
 71         }
 72         int e = st;
 73         VII ans;
 74         if (sz) {
 75             while (1) {
 76                 int a = v[st].first, b = v[(st + 1) % sz].first;
 77                 int l = v[st].second, r = v[(st + 1) % sz].second;
 78                 if (a == b) {
 79                     ok = 0;
 80                     break;
 81                 } else if (a == 1) {
 82                     for (; l != r; (l += 1) %= n)ans.pb(mp(l, (l + 1) % n));
 83                 } else {
 84                     for (; r != l; (r += n - 1) %= n)ans.pb(mp(r, (r + n - 1) % n));
 85                 }
 86                 (st += 2) %= sz;
 87                 if (st == e)break;
 88             }
 89         }
 90         if (!ok) {
 91             gao();
 92             return;
 93         }
 94         out << "YES" << endl << ans.size() << endl;
 95         rep(i, ans.size()) {
 96             out << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
 97         }
 98     }
 99 };
100
101 int main() {
102     std::ios::sync_with_stdio(false);
103     std::cin.tie(0);
104     hdu5353 solver;
105     std::istream &in(std::cin);
106     std::ostream &out(std::cout);
107     int n;
108     in >> n;
109     for (int i = 0; i < n; ++i) {
110         solver.solve(in, out);
111     }
112
113     return 0;
114 }
时间: 2024-12-14 18:48:16

hdu5353 Average(模拟)的相关文章

HDOJ 5353 Average 模拟

各种情况特判,然后枚举前两个点之间的关系 Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1723    Accepted Submission(s): 438 Special Judge Problem Description There are n soda sitting around a round tabl

HDU5353:Average(贪心)

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 numbe

training 2

Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.136 Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.000 Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.198 Average Precision (AP)

[hdu5353]模拟

题意:有n个小朋友,每个小朋友手上有一些糖,考虑每两个相邻的小朋友a.b,可以选择执行3种操作中的任一种:(1)a给b一粒糖(2)b给a一粒糖(3)不进行任何动作,问能否通过确定每两个相邻的小朋友的操作使得最终每个人的糖果数量相等. 思路:如果只有1个小朋友,那么肯定是可行的,如果糖果数总和取模小朋友数不为0,那么肯定是不可行的.令第i个小朋友的糖果数为a[i],首先将平均值ave计算出来,然后a[i]=a[i]-ave.注意到a[i]的值是确定的,且每个a[i]到达的目标值也是确定的,也就是0

hdu5353(2015多校6)--Average(贪心)

题目链接:点击打开链接 题目大意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块. 现在有三种操作: 第i个人给第i+1个人一块.如果i有 第i+1个人给第i个人一块.如果i+1有 什么都不做. 第i个人和第i+1个人之间,可以选择一种操作并执行,问最终能不能让所有人手里的糖相等. 当n = 1 时,永远是YES 当n = 2 时,注意1和2之间只能有一种操作,不存在循环. 当n > 2 时: 糖的总数不能均分到n个人手中,直接NO 可以分开,存在一个循环,那么枚举第1个人对第2

[Linux运维]常用场景模拟 -- cpu使用率模拟

[Linux运维]常用场景模拟 -- cpu使用率模拟 from http://www.cnblogs.com/zk47/p/4771105.html 1 单个核 100%: 代码 kill_cpu.c #include <stdlib.h> int main() { while(1); return 0; } 运行 $ gcc -o out kill_cpu.c $ ./out 看top的结果: $ top top - 15:44:08 up 207 days, 21:29, 2 users

理解Load Average做好压力测试(转)

转载自:http://www.blogjava.net/cenwenchu/archive/2008/06/30/211712.html SIP的第四期结束了,因为控制策略的丰富,早先的的压力测试结果已经无法反映在高并发和高压力下SIP的运行状况,因此需要重新作压力测试.跟在测试人员后面做了快一周的压力测试,压力测试的报告也正式出炉,本来也就算是告一段落,但第二天测试人员说要修改报告,由于这次作压力测试的同学是第一次作,有一个指标没有注意,因此需要修改几个测试结果.那个没有注意的指标就是load

poj 2501 Average Speed

Average Speed Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4842   Accepted: 2168 Description You have bought a car in order to drive from Waterloo to a big city. The odometer on their car is broken, so you cannot measure distance. But

《C++ Primer Plus》12.7 队列模拟 学习笔记

Heather银行打算在Food Heap超市开设一个自动柜员机(ATM).Food Heap超市的管理者担心排队使用ATM的人流会干扰超市的交通,希望限制排队等待的人数.Heather银行希望对顾客排队等待的事件进行估测.要编写一个程序来模拟这种情况,让超市的管理者可以了解ATM可能招骋的影响.对于这种问题,最自然的方法是使用顾客对列.队列是一种抽象的数据类型(Abstract Data Type,ADT),可以存储有序的项目序列.新项目被添加在队尾,并可以删除队首的项目.队列有点像栈,单栈在