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 ...
随机推荐
- python functools.lru_cache做备忘功能
import time import functools def clock(func): @functools.wraps(func)#还原被装饰函数的__name__和__doc__属性 def ...
- 201621123043 《Java程序设计》第3周学习总结
1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系.步骤如下: 1.1 写出你 ...
- 用python实现简单购物车功能
all_asset = 0 i1 = input("请输入总资产:") all_asset = int(i1) goods = [ {'name':'电脑','price':199 ...
- python 操作Memcached
启动Memcached memcached -d -m 10 -u root -l 10.211.55.4 -p 12000 -c 256 -P /tmp/memcached.pid 参数说明: -d ...
- 十、Python练习----基础搭建飞机大战
只是简单的学习了pygame,实现飞机的摧毁还需要多张图片的切换,和sprite(碰撞精灵),还有多种音效的添加(如背景音乐.摧毁特效).以后再深入学习我只是练习一下python. 一.搭建界面(基于 ...
- JAVA线程池原理详解(1)
线程池的优点 1.线程是稀缺资源,使用线程池可以减少创建和销毁线程的次数,每个工作线程都可以重复使用. 2.可以根据系统的承受能力,调整线程池中工作线程的数量,防止因为消耗过多内存导致服务器崩溃. 线 ...
- JAVA_SE基础——43.抽象类
高手勿喷~ 抽象类:当定义一个类时,常常需要定义一些方法来描述该类的行为特征,但有时这些方法的实现方式是无法确定的.例如定义Animal类时,shout()方法用于表示动物的叫声,但是针对不同的动物, ...
- 详解k8s零停机滚动发布微服务 - kubernetes
1.前言 在当下微服务架构盛行的时代,用户希望应用程序时时刻刻都是可用,为了满足不断变化的新业务,需要不断升级更新应用程序,有时可能需要频繁的发布版本.实现"零停机"." ...
- mysql 存储过程 实现数据同步
数据库 表 发生变化,需要把2.0的表数据 同步到3.0库中去: -- 同步数据存储过程执行 -- 更新留言旧表数据到新表数据中 /*DEFINER:Vector*/ drop procedure i ...
- Struts(十五):主题
Theme主题是配置的struts-core.jar下的com.apache.struts2包下的default.properties中(默认配置为:xhtml) ### Standard UI th ...