题意:有n个小朋友,每个小朋友手上有一些糖,考虑每两个相邻的小朋友a、b,可以选择执行3种操作中的任一种:(1)a给b一粒糖(2)b给a一粒糖(3)不进行任何动作,问能否通过确定每两个相邻的小朋友的操作使得最终每个人的糖果数量相等。

思路:如果只有1个小朋友,那么肯定是可行的,如果糖果数总和取模小朋友数不为0,那么肯定是不可行的。令第i个小朋友的糖果数为a[i],首先将平均值ave计算出来,然后a[i]=a[i]-ave。注意到a[i]的值是确定的,且每个a[i]到达的目标值也是确定的,也就是0,那么有一个显然的性质,如果一直a[i]给了a[i+1]x粒糖,那么相当于就知道了a[i+1]给了a[i+2]多少粒糖,因为要保证a[i+1]等于0,所以这个值是确定的,类似这样可以得到全部的a[i]给了a[i+1]多少粒糖(0表示没给,-1表示接受了对方给的1粒糖),同时如果中途出现某个a[i]不为0,则可以立即判定当前是无解的。于是只需枚举a[0]给了a[1]多少粒糖(只有-1,0,1三种),然后一边递推一边检查矛盾即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
#define X                   first
#define Y                   second
#define pb                  push_back
#define mp                  make_pair
#define all(a)              (a).begin(), (a).end()
#define fillchar(a, x)      memset(a, x, sizeof(a))
 
typedef long long ll;
typedef pair<intint> pii;
typedef unsigned long long ull;
 
#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}
 
const double PI = acos(-1.0);
const int INF = 1e9 + 7;
 
/* -------------------------------------------------------------------------------- */
 
const int maxn = 1e5 + 7;
 
int n, m;
pii ans[maxn];
int a[maxn], b[maxn];
 
bool chk(int x) {
    memcpy(b, a, sizeof(a));
    b[0] -= x;
    b[1] += x;
    m = 0;
    if (x == 1) ans[m ++] = mp(0, 1);
    if (x == -1) ans[m ++] = mp(1, 0);
    for (int i = 1; i < n; i ++) {
        int v = (i + 1) % n;
        if (abs(b[i]) > 1) return false;
        if (b[i] == 1) {
            b[v] ++;
            ans[m ++] = mp(i, v);
        }
        if (b[i] == -1) {
            b[v] --;
            ans[m ++] = mp(v, i);
        }
    }
    return true;
}
 
bool work() {
    if (abs(a[0]) > 2) return false;
    if (chk(1) || chk(0) || chk(-1)) return true;
    return false;
}
bool all_zero() {
    for (int i = 0; i < n; i ++) {
        if (b[i]) return false;
    }
    return true;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt""r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    int T;
    cin >> T;
    while (T --) {
        cin >> n;
        ll sum = 0;
        for (int i = 0; i < n; i ++) {
            scanf("%d", a + i);
            b[i] = a[i];
            sum += a[i];
        }
        if (sum % n) puts("NO");
        else {
            if (n == 1 || all_zero()) {
                puts("YES");
                puts("0");
                continue;
            }
            int ave = sum / n;
            for (int i = 0; i < n; i ++) a[i] -= ave;
            m = 0;
            if (work()) {
                puts("YES");
                printf("%d\n", m);
                for (int i = 0; i < m; i ++) printf("%d %d\n", ans[i].X + 1, ans[i].Y + 1);
            }
            else puts("NO");
        }
    }
    return 0;
}

[hdu5353]模拟的更多相关文章

  1. hdu5353 Average(模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Average Time Limit: 4000/2000 MS (Java/Ot ...

  2. App开发:模拟服务器数据接口 - MockApi

    为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...

  3. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  4. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  5. HTML 事件(四) 模拟事件操作

    本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4.  ...

  6. 模拟AngularJS之依赖注入

    一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...

  7. webapp应用--模拟电子书翻页效果

    前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...

  8. javascript动画系列第一篇——模拟拖拽

    × 目录 [1]原理介绍 [2]代码实现 [3]代码优化[4]拖拽冲突[5]IE兼容 前面的话 从本文开始,介绍javascript动画系列.javascript本身是具有原生拖放功能的,但是由于兼容 ...

  9. C++ 事件驱动型银行排队模拟

    最近重拾之前半途而废的C++,恰好看到了<C++ 实现银行排队服务模拟>,但是没有实验楼的会员,看不到具体的实现,正好用来作为练习. 模拟的是银行的排队叫号系统,所有顾客以先来后到的顺序在 ...

随机推荐

  1. jdbctemplate打印sql

    在logback.xml里加入如下配置即可: <include resource="org/springframework/boot/logging/logback/base.xml& ...

  2. WEBMIN(CVE-2019-15107) 学习

    简单介绍: Webmin是目前功能最强大的基于Web的Unix系统管理工具.管理员通过浏览器访问Webmin的各种管理功能并完成相应的管理动作.目前Webmin支持绝大多数的Unix系统,这些系统除了 ...

  3. [YII2.0] 高级模板简单安装教程

    YIICHINA官网教程就很完善:http://www.yiichina.com/tutorial/692 但是在yii2框架安装运行init.bat报错php.exe不是内部或外部命令, 解决办法: ...

  4. 基于udp协议的套接字通信

    服务端: import socket server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) server.bind(('127.0.0.1',8 ...

  5. 团队题目——TD课程通

    一.团队介绍 团队名称:精神小伙成双队 团队成员:冯静妃(博客地址:https://www.cnblogs.com/fengjingfei/) 李佳伟(博客地址:https://www.cnblogs ...

  6. PHP的yield是个什么玩意

    来源:https://segmentfault.com/a/1190000018457194 其实,我并不是因为迭代或者生成器或者研究PHP手册才认识的yield,要不是协程,我到现在也不知道PHP中 ...

  7. CSRF与平行越权的区别

    .CSRF攻击者不需要登录,越权攻击者也得登录,只是没有做针对性的控制: .CSRF攻击者自己不访问受攻击页面,诱导受害者在登录被攻击系统后点击攻击页面:越权攻击者可以直接访问受攻击页面: .CSRF ...

  8. Git 简明手册

    0,Git 是什么 Git 是一个VCS(Version Control System),即版本控制系统. 版本控制系统从字面意思来看,它的用途就是管理/控制文件的版本.使用它,可以方便的知道一个文件 ...

  9. Scala教程之:Enumeration

    Enumeration应该算是程序语言里面比较通用的一个类型,在scala中也存在这样的类型, 我们看下Enumeration的定义: abstract class Enumeration (init ...

  10. Django中search fields报错:related Field has invalid lookup: icontains

    models.py 文件 # coding:utf8from django.db import models class Book(models.Model):        name = model ...