2015 多校联赛 ——HDU5353(构造)
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.
6
1 0 1 0 0 0
5
1 1 1 1 1
3
1 2 3
YES
0
YES
2
2 1
3 2
对相邻两个数之间进行以下三种操作的一种,最后使他们相等
①a++ b-- ②a-- b++ ③nothing
如果(sum%n != 0),直接失败。用一个数组来记录数与平均数之间的差值。
先枚举第一位数的三种情况,
当C[i] == 1时,从C[i+1]取一;
当C[i] == -1时,给C[i+1]一个;
当C[i] == 0时,nothing;
else:false。
ps:完全没想到要对第一位进行枚举 OoO,一直wa
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
const int mod = 1000000007;
int a[100050];
int aver,flag,n;
int p[100050][2];
int c[100050];
int tot; bool solve()
{
for(int i = 3; i <= n; i++)
c[i] = a[i];
for(int i = 2; i <= n; i++)
{
if(c[i] == 0)
continue;
else if(c[i] == 1 && i != n)
{
c[i+1]++;
c[i]--;
p[tot][0] = i;
p[tot++][1] = i+1;
}
else if(c[i] == 1 && i == n)
{
c[1]++;
c[i]--;
p[tot][0] = i;
p[tot++][1] = 1;
}
else if(c[i] == -1 && i!= n)
{
c[i]++;
c[i+1]--;
p[tot][0] = i+1;
p[tot++][1] = i;
}
else if(c[i] == -1 && i== n)
{
c[i]++;
c[1]--;
p[tot][0] = 1;
p[tot++][1] = i;
}
else if(c[i] >1 || c[i] < -1)
return false;
}
for(int i = 1; i <= n; i++)
if(c[i]!=0)
return false;
return true;
} void prin()
{
printf("YES\n");
printf("%d\n",tot);
for(int i = 0; i < tot; i++)
printf("%d %d\n",p[i][0],p[i][1]);
} int main()
{
int T;
//freopen("01.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ll sum = 0;
memset(p,0,sizeof(p));
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
sum += a[i];
} if(sum % n)
{
printf("NO\n");
continue;
}
aver = sum / n;
flag = 0;
tot = 0;
for(int j = 1; j <= n; j++)
a[j] =a[j] - aver;
c[1] = a[1];
c[2] = a[2];
if(solve())
{
prin();
}
else
{
tot = 0;
c[1] = a[1] - 1;
c[2] = a[2] + 1;
p[tot][0] = 1;
p[tot++][1] = 2;
if(solve())
prin();
else
{
tot = 0;
c[1] = a[1] + 1;
c[2] = a[2] - 1;
p[tot][0] = 2;
p[tot++][1] = 1;
if(solve())
prin();
else
printf("NO\n");
}
} }
return 0;
}
2015 多校联赛 ——HDU5353(构造)的更多相关文章
- 2015 多校联赛 ——HDU5334(构造)
Virtual Participation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 2015 多校联赛 ——HDU5302(构造)
Connect the Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2015 多校联赛 ——HDU5294(最短路,最小切割)
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 2015 多校联赛 ——HDU5325(DFS)
Crazy Bobo Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Tota ...
- 2015 多校联赛 ——HDU5316(线段树)
Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...
- 2015 多校联赛 ——HDU5323(搜索)
Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 2015 多校联赛 ——HDU5319(模拟)
Painter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- 2015 多校联赛 ——HDU5301(技巧)
Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...
- 2015 多校联赛 ——HDU5303(贪心)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
随机推荐
- Alpha冲刺Day10
Alpha冲刺Day10 一:站立式会议 今日安排: 由林静完成第三方机构的用户信息管理模块 由张梨贤完成第三方机构的委托授权管理模块 由黄腾飞和周静平完成政府人员模块下风险管控子模块下的核实企业风险 ...
- 20162328蔡文琛week06
学号 2016-2017-2 <程序设计与数据结构>第X周学习总结 教材学习内容总结 继承是从已有类派生出一个新类的过程. 继承的目的之一之复用已有的软件. 继承呢在子类和父类见建立了is ...
- C++数据结构中的基本算法排序
冒泡排序 基本思想:两两比较待排序的数,发现反序时交换,直到没有反序为止. public static void BubbleSort(int[] R) { for (int i = 0; i < ...
- es6+react.js组件入门初探
React是一个用于构建用户见面的javascript库. React主要用于构建UI,许多人认为React是MVC中的V(视图) React起源于Facebook的内部项目,用来架设Instagra ...
- linux cenots7安装mysql
1.下载mysql 下载的话先确认好版本. system:centos7 mysql:5.7 下面的版本自己选择,一般是86位的. 下载好的文件 2.上传到服务器 soft文件夹,终端也进入了 ...
- win-zabbix_agent端配置解析
Zabbix agent 在windows上安装部署 部署windows服务器需要在agent服务器上添加到10.4.200.2的路由 蓝泰服务器需要添加10.0.90.5的网关,联通的机器需要添加到 ...
- Python入门之PyCharm的快捷键与常用设置和扩展(Win系统)
1. PyCharm的快捷键 2 . PyCharm的常用设置和扩展 ---------------------------------------------------------------- ...
- Intent 的两种主要使用方法
首先建立两个activity界面 Activity1如下 public class MainActivity extends AppCompatActivity { private Button bt ...
- Python系列之 - 面向对象(1)
python是一门面向对象的编程语言,python中的一切均是对象. 有对象就提到类,对象和类就像是儿子和老子的关系,是不可分的一对. 什么是类 类就是具有一些共同特性的事物的统称.好比人类, ...
- 真正理解拉格朗日乘子法和 KKT 条件
这篇博文中直观上讲解了拉格朗日乘子法和 KKT 条件,对偶问题等内容. 首先从无约束的优化问题讲起,一般就是要使一个表达式取到最小值: \[min \quad f(x)\] 如 ...