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
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.
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.
不能被整除,以及与平均值的差值超过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(模拟)的更多相关文章
- hdu5353 Average
Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...
- STL复习之 map & vector --- disney HDU 2142
题目链接: https://vjudge.net/problem/40913/origin 大致题意: 这是一道纯模拟题,不多说了. 思路: map模拟,vector辅助 其中用了map的函数: er ...
- training 2
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.136 Average Precision (AP) @[ IoU ...
- Average(模拟)
Average Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- HDU 5353 Average 糖果分配(模拟,图)
题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路: 明显每个人最多只能多于平均值2个 ...
- [hdu5353]模拟
题意:有n个小朋友,每个小朋友手上有一些糖,考虑每两个相邻的小朋友a.b,可以选择执行3种操作中的任一种:(1)a给b一粒糖(2)b给a一粒糖(3)不进行任何动作,问能否通过确定每两个相邻的小朋友的操 ...
- Lucky and Good Months by Gregorian Calendar - POJ3393模拟
Lucky and Good Months by Gregorian Calendar Time Limit: 1000MS Memory Limit: 65536K Description Have ...
- HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Relative atomic mass Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 理解Load Average做好压力测试
http://www.blogjava.net/cenwenchu/archive/2008/06/30/211712.html CPU时间片 为了提高程序执行效率,大家在很多应用中都采用了多线程模式 ...
随机推荐
- ECSTORE 货币格式
世界上许多国家都有不同的货币 格局和数字 格局 特例 .针对特定的当地化环境正确地 格局化和显示货币是当地化的一个主要部分,ecstore 可以同过后台的设置,来更改货币的格式,具体方式为 后台-&g ...
- 自动启动docker container
当系统启动的时候要启动docker container, 可以利用systemctl来实现 比如拿mongodb为例 创建 /usr/lib/systemd/system/docker_mongodb ...
- ucos 创建 空闲任务的目的
几乎任何操作系统都需要有空闲任务. 因为CPU(提供CPU级休眠的不算)没办法停下来,尤其是嵌入式系统这一块. CPU停下来的唯一情况就是断电了,而要保持操作系统任何时候都能及时的对外做出响应,就必须 ...
- 将主机IDS OSSEC日志文件存入MYSQL的方法
将主机IDS OSSEC日志文件存入MYSQL的方法 http://www.freebuf.com/articles/system/6139.html http://ossec-docs.readth ...
- BZOJ 1492 货币兑换Cash
http://www.lydsy.com/JudgeOnline/problem.php?id=1492 思路: 问题转变为维护一个凸包,每次转移都找凸包上的点,并更新凸壳 可以用splay维护,或者 ...
- Codeforces 527D Clique Problem
http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...
- WCF 基于Cookie的登录验证回传问题的解决
参考资料: http://www.cnblogs.com/czcz1024/p/3333138.html http://megakemp.com/2009/02/06/managing-shared- ...
- windows7环境下 硬盘安装ubuntu 12.04 server版
之前一直用windows7环境下的虚拟机装的操作系统,但有时候在切换系统时老是死机,还是装一个硬盘版的ubuntu 12.04 server吧 先说一下本人的环境吧:windows 7 32位专业版+ ...
- AzCopy – 跨帐户复制 Blob
您可以随时从 aka.ms/AzCopy 下载最新版本. 去年4月发布的版本中的新增功能 支持跨帐户复制 Blob:AzCopy 允许您在相同存储帐户内或不同存储帐户之间复制 Blob(有关跨帐户 B ...
- 处理通过<input type="file">的Post 请求
[HttpPost] public ActionResult Cal() { string ExcelName = System.DateTime.Now.ToString("yyyyMMd ...