博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2015 多校联赛 ——HDU5353(构造)
阅读量:7069 次
发布时间:2019-06-28

本文共 3153 字,大约阅读时间需要 10 分钟。

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 
#include
#include
#include
#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;}

  

 

转载于:https://www.cnblogs.com/Przz/p/5409798.html

你可能感兴趣的文章
通用业务系统基础平台(五) 工作流系统
查看>>
H5 App如此强悍,要降薪的恐怕已不只是iOS程序员
查看>>
[国家集训队2012]middle
查看>>
SPOJ 1812 Longest Common Substring II
查看>>
vue双向绑定原理
查看>>
路飞学城-python爬虫密训-第一章
查看>>
Python输入输出练习,运算练习,turtle初步练习
查看>>
Android进阶学习
查看>>
用jedis获取redis连接(集群和非集群状态下)
查看>>
html 调用qq客服
查看>>
初学python,感受和C的不同
查看>>
OpenLDAP在LINUX下的安装说明
查看>>
洛谷P1582 倒水
查看>>
洛谷P3146 [USACO16OPEN]248
查看>>
Codeforces Round #419 (Div. 2) A-E
查看>>
【Leetcode】Path Sum II
查看>>
asp.net 2.0 导出DataTable到Excel中
查看>>
PCA算法学习_1(OpenCV中PCA实现人脸降维)
查看>>
Kinect+OpenNI学习笔记之12(简单手势所表示的数字的识别)
查看>>
对比学习UIKit和AppKit--入门级
查看>>