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时间片 为了提高程序执行效率,大家在很多应用中都采用了多线程模式 ...
随机推荐
- mac版sublime text2包管理器安装步骤
第一步: control+-打开命令执行窗口. 第二步: 将包管理器的代码复制到命令执行窗口: import urllib2,os,hashlib; h = '2915d1851351e5ee549c ...
- Animation 案例解释
Animation 案例解释: ------------摘自W3c 过度动画类型: linear:线性过渡.等同于贝塞尔曲线(0.0, 0.0, 1.0, 1.0) ease:平滑过渡.等同于贝塞尔曲 ...
- Robot Framework自动化测试---元素定位
不要误认为Robot framework 只是个web UI测试工具,更正确的理解Robot framework是个测试框架,之所以可以拿来做web UI层的自动化是国为我们加入了selenium2的 ...
- Thinking in Java——笔记(21)
Concurrency However, becoming adept at concurrent programming theory and techniques is a step up fro ...
- Idea使用记录--添加Problems&&解决Autowired报错could not autowire
今天在使用Idea的时候,发现Idea在整个每次找到错误代码非常不方便(Idea如果类中有错误,没有打开过类并不会提示,比如构建工程后缺少jar包问题).我想快速看到工程哪里出问题类似于eclipse ...
- 主题模型-LDA浅析
(一)LDA作用 传统判断两个文档相似性的方法是通过查看两个文档共同出现的单词的多少,如TF-IDF等,这种方法没有考虑到文字背后的语义关联,可能在两个文档共同出现的单词很少甚至没有,但两个文档是相似 ...
- IOS 通过button获取cell
在使用tableview时,有时我们需要在cell中添加button和label,以便添加某项功能,而且往往点这个button的方法中需要知道button所在cell中label内存放的值. 一般而言 ...
- 自制单片机之七……扩展:DS18B20温度测量
DS18B20数字温度测量传感器,网上介绍很多,我就不罗嗦了.见图 DS18B20与前产品DS1820的不同: DS18B20继承了DS1820的全部优点,并做了如下改进 1.供电范围扩大为3.0-- ...
- C#进程间通信--API传递参数(SendMessage)
原文 C#进程间通信--API传递参数(SendMessage) 我们不仅可以传递系统已经定义好的消息,还可以传递自定义的消息(只需要发送消息端和接收消息端对自定义的消息值统一即可).下面的发送和接 ...
- 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应
http://blog.csdn.net/panjunbiao/article/details/8615880 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应