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 ...
随机推荐
- 201621123062《Java程序设计》第一周学习总结
1.本周学习总结 关键词: 初步熟悉Java的基本组成.语言特点(简单性.结构中立性).运行环境.简单语法等. 关键概念之间的联系: 1.JVM是Java程序唯一认识的操作系统,其可执行文件为.cla ...
- tableView//collectionView加载时的动画
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( ...
- 从PRISM开始学WPF(六)MVVM(二)Command?
从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ...
- jiVMware的网络配置Linux
需求需要配置VMware的虚拟Linux的ip以达到本地可以访问,而且虚拟机Linux可以上网: 第一方案:选择桥接模式 思路:因为桥接可以,使得虚拟机Linux把本地当做一座桥一样连接到路由器,然后 ...
- Angular 学习笔记 ( CDK - Portal )
Portal 的主要使用场景是 dynamic component 动态的插入模板或组件. Portal 可分为 2 种. 进入和出去 (in or out) ComponentPortal, Tem ...
- Python模块 - os , sys.shutil
os 模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录: ...
- ORA-01578和ORA-26040--NOLOGGING操作引起的坏块-错误解释和解决方案(文档ID 1623284.1)
ORA-01578和ORA-26040--NOLOGGING操作引起的坏块-错误解释和解决方案(文档ID 1623284.1) (一)NOLOGGING操作引起的坏块(ORA-01578和ORA-26 ...
- php 数组对象之间的转换
在之前我写过php返回json数据简单实例 从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码. 一.json_encode() 1 ...
- AOP及专有名词通俗解答
AOP面向切面编程,是一种编程思想,并不是Spring专有,Spring是封装代理模式完成,之前的博客中也写到了关于AOP的文章,Filter和代理,请见<以此之长,补彼之短----AOP(Fi ...
- Linux环境下用C语言实现socket 通信---简单代码
Socket编程实例: 服务器端:一直监听本机的8000号端口,如果收到连接请求,将接收请求并接收客户端发来的消息,并向客户端返回消息. 客户端:client.c /* File Name: clie ...