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 ...
随机推荐
- 团队作业4——第一次项目冲刺(Alpha版本) Day 2
小队@JMUZJB-集美震惊部 一.Daily Scrum Meeting照片 二.Burndown Chart 燃尽图 三.项目进展 成员 工作 丘雨晨 环境配置 刘向东 数据库搭建,环境配置 江泽 ...
- 设计模式NO.3
设计模式NO.3 本次博客内容为第三次设计模式的练习.根据老师的要求完成下列题目: 题目1 某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商 ...
- Java基础 成员变量的继承与覆盖
通过继承可以得到父类的成员变量,子类的成员变量包括从父类继承的成员变量(包括从祖先类中继承的成员变量)以及子类中重新定义的成员变量.本次介绍内容包括:可以继承哪些成员?如果子类和父类出现了相同的成员变 ...
- ThreadLocal就是这么简单
前言 今天要研究的是ThreadLocal,这个我在一年前学习JavaWeb基础的时候接触过一次,当时在baidu搜出来的第一篇博文ThreadLocal,在评论下很多开发者认为那博主理解错误,给出了 ...
- MSIL实用指南-一维数组的操作
本篇讲解怎么生成和操作一维数组.各种数组类型创建的步骤是一样的,但是加载和保存步骤有所不同. 一.创建数组所有类型的一维数组创建都是一样的,分三步.1.加载数组长度2.生成指令 Newarr < ...
- eclipse maven项目目录
今天遇见一个错误,关于eclipse项目的路径问题,web-inf的路径,上图和下图出现了两种web-INF,src的web-INFf和webContent的web-INF,src里面的文件需要编译以 ...
- 初学深度学习(TensorFlow框架的心得and经验总结)自用环境的总结
初学者的时间大部分浪费在了环境上了: 建议直接上Linux系统,我推荐国产的深度系统,deepin这几年一直在不断的发展,现在15.4已经很不错了 1,图形化界面很漂亮,内置正版crossover,并 ...
- Python内置函数(28)——iter
英文文档: iter(object[, sentinel]) Return an iterator object. The first argument is interpreted very dif ...
- JAVA工程师面试题【来自并发编程网】
基础题: Java线程的状态 进程和线程的区别,进程间如何通讯,线程间如何通讯 HashMap的数据结构是什么?如何实现的.和HashTable,ConcurrentHashMap的区别 Cookie ...
- 图数据库orientDB(1-2)例子
http://gog.orientdb.com/index.html#/infotab 小朱25岁,出生在教师家庭并且有个姐姐小田,他现在奋斗在帝都. 那么SQL是这样滴!!! CREATE VER ...