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.

Sample Input
3
6
1 0 1 0 0 0
5
1 1 1 1 1
3
1 2 3
 
Sample Output
NO
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(构造)的更多相关文章

  1. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  2. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. 2015 多校联赛 ——HDU5294(最短路,最小切割)

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  4. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  5. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  6. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  8. 2015 多校联赛 ——HDU5301(技巧)

    Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...

  9. 2015 多校联赛 ——HDU5303(贪心)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

随机推荐

  1. 2017-2018-1 我爱学Java 第六七周 作业

    团队六七周作业 完善版需求规格说明书 制定团队编码规范 数据库设计 后端架构设计 TODOList 参考资料 完善版需求规格说明书 <需求规格说明书>初稿不足之处: 1.开发工具写错 2. ...

  2. 201421123042 《Java程序设计》第8周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码: 答:查找 ...

  3. Flask 学习 十三 应用编程接口

    最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. REST的六个特性: 客户端-服务器(Client-Server)服务器 ...

  4. php后台的在控制器中就可以实现阅读数增加

    $smodel=M('Sswz');$smodel->where($map)->setInc('view' ,1);php后台的在控制器中就可以实现阅读数增加前台不需要传值

  5. Struts2 配置文件小结

    每次写的博文都被管理员都被移出首页,好气!还希望有哪位大神可以指点迷津-- struts2 配置文件的 result 节点 result 节点是 action 节点的子节点,他代表着 action 方 ...

  6. jhipster生成项目无法使用restful请求,报access_denied 403错误

    写在前边: 我们的微服务是注册中心.uaa.gateway为基础,添加微服务应用,昨天下午在测试jhipster的增删改查,因为jhipster生成的代码都是restful的,好不容易找到网关配置的映 ...

  7. 新概念英语(1-115)Knock! Knock!

    Lesson 115 Knock, knock! 敲敲门! Listen to the tape then answer this question. What does Jim have to dr ...

  8. 新概念英语(1-9)How is Ema?

    A:Hello Helen. B:Hi Steven. A:How are you today? B:I'm very well, thank you. And you? A:I'm fine tha ...

  9. Web框架之Django基础篇

    Web框架之Django基础篇   本节介绍Django 简介,安装 基本配置及学习  路由(Urls).视图(Views).模板(Template).Model(ORM). 简介 Django 是一 ...

  10. mysql的账户管理

    mysql中账户管理:1 查看所有用户: 所有用户及权限信息都存储在mysql数据库中的user表中 查看user表的结构 desc user\G; 主要字段: host: 表示允许访问的主机 use ...