hdu5353||2015多校联合第六场1001 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5353

Problem Description

There are n soda
sitting around a round table. soda are numbered from 1 to n and i-th
soda is adjacent to (i+1)-th
soda, 1-st
soda is adjacent to n-th
soda.

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.

Now you are to determine whether it is possible and give a sequence of operations.

Input

There are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case:

The first contains an integer n (1≤n≤105),
the number of soda.

The next line contains n integers a1,a2,…,an (0≤ai≤109),
where ai denotes
the candy i-th
soda has.

Output

For each test case, output "YES" (without the quotes) if possible, otherwise output "NO" (without the quotes) in the first line. If possible, then the output an integer m (0≤m≤n) in
the second line denoting the number of operations needed. Then each of the following m lines
contain two integers x and y (1≤x,y≤n),
which means that x-th
soda gives y-th
soda a candy.

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
/**
hdu5353||2015多校联合第六场1001  贪心
题目大意:给定一个序列首尾相连,每两个临的数可以相互给出一个或接受一个1,问是否可以经过一系列转移使所有的数相等
解题思路:枚举第一个数和最后一个数之间的关系(给出一个,接受一个,不处理),然后从前往后判断i和i+1的关系,i少1则从i+1得1,
          多1则给i+1一个,正好则跳过。处理的时候就不用考虑i-1了,最后判断是否成功即可。复杂度O(n*3)
*/
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long LL;
int a[100005],b[100005];
int n,k,p[100005][2];

bool judge()
{
    memcpy(b,a,sizeof(a));
    for(int i=0;i<n-1;i++)
    {
        if(b[i]<0)
        {
            b[i]++;
            b[i+1]--;
            p[k][0]=i+2;
            p[k++][1]=i+1;
        }
        else if(b[i]>0)
        {
            b[i]--;
            b[i+1]++;
            p[k][0]=i+1;
            p[k++][1]=i+2;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(b[i]!=0)return 0;
    }
    return 1;
}

void print(int flag)
{
    if(flag)
    {
        printf("YES\n%d\n",k);
        for(int i=0; i<k; i++)
        {
            printf("%d %d\n",p[i][0],p[i][1]);
        }
    }
    else
    {
        puts("NO");
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        LL sum=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(sum%n)
        {
            puts("NO");
            continue;
        }
        sum/=n;
        if(n==2&&max(a[1],a[0])-min(a[1],a[0])>2)
        {
            puts("NO");
            continue;
        }
        for(int i=0; i<n; i++)
        {
            a[i]-=sum;
        }
        k=0;
        int flag=0;
        if(judge())
        {
            flag=1;
            print(1);
        }
        else
        {
            k=0;
            p[k][0]=n,p[k++][1]=1;
            a[n-1]--,a[0]++;
            if(judge())
            {
                flag=1;
                print(1);
            }
            else
            {
                k=0;
                p[k][0]=1,p[k++][1]=n;
                a[0]-=2,a[n-1]+=2;
                if(judge())
                {
                    flag=1;
                    print(1);
                }
            }
        }
        if(flag==0)
        {
            print(0);
        }
    }
    return 0;
}

Problem Description

There are n soda
sitting around a round table. soda are numbered from 1 to n and i-th
soda is adjacent to (i+1)-th
soda, 1-st
soda is adjacent to n-th
soda.

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.

Now you are to determine whether it is possible and give a sequence of operations.

Input

There are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case:

The first contains an integer n (1≤n≤105),
the number of soda.

The next line contains n integers a1,a2,…,an (0≤ai≤109),
where ai denotes
the candy i-th
soda has.

Output

For each test case, output "YES" (without the quotes) if possible, otherwise output "NO" (without the quotes) in the first line. If possible, then the output an integer m (0≤m≤n) in
the second line denoting the number of operations needed. Then each of the following m lines
contain two integers x and y (1≤x,y≤n),
which means that x-th
soda gives y-th
soda a candy.

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 06:32:56

hdu5353||2015多校联合第六场1001 贪心的相关文章

2014多校联合-第六场

最近这两场好无奈啊... 今天这场最后30分钟敲1001,压力倍增,虽然思路比较明确,但是代码打起来不怎么容易. 但是还是好在25分钟左右debug结束.提交wa,再提交,依然WA.......最后5分钟,还是没有AC掉. 一开始以为是精度问题,后来才sb的发现原来数组开小了. 在压力环境下保证代码的效率和质量真不是一件容易的事情.不过数组开小了,真是不可原谅. 1001:Map 题目相当于几条链表.把链表排成几行. 然后枚举每一列的状态会被操作多少次. 然后把和累加起来,然后直接除以状态总数.

hdu5379||2015多校联合第7场1011 树形统计

http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in the yard doesn't look good. So he wants to decorate the tree.(The tree has n vertexs, i

HDOJ多校联合第六场

先一道一道题慢慢补上, 1009.题意,一棵N(N<=50000)个节点的树,每个节点上有一个字母值,给定一个串S0(|S0| <=30),q个询问,(q<=50000),每次询问经过两个点u,v之间的路径上的字母构成字符串S,问S0在S中作为序列出现了多少次. 分析:对于每次询问需要知道其LCA,设w = LCA(u, v),可以用tarjan处理出来,或者倍增法也行.然后w会将S0分成两部分,记做S1,S2,然后分别考虑S1在u->w的路径出现次数,以及S2在v->w出现

hdu5360||多校联合第6场1008 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5360 Problem Description There are n soda conveniently labeled by 1,2,-,n. beta, their best friends, wants to invite some soda to go hiking. The i-th soda will go hiking if the total number of soda that go hi

2015多校联合训练第一场Tricks Device(hdu5294)

题意:给一个无向图,给起点s,终点t,求最少拆掉几条边使得s到不了t,最多拆几条边使得s能到t 思路: 先跑一边最短路,记录最短路中最短的边数,总边数-最短边数就是第二个答案 第一个答案就是在最短路里面求最小割,也就是求最大流,然后根据最短路在建个新图,权为1,跑一边网络流 模板题,以后就用这套模板了 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #incl

2015多校联合训练第一场Assignment(hdu5289)三种解法

题目大意:给出一个数列,问其中存在多少连续子序列,子序列的最大值-最小值 #include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cmath> using namespace std; int maxsum[100000][30]; int minsum[100000][30]; int a[100000]; int n,k; v

HDU 5793 A Boring Question (费马小定理) ---2016杭电多校联合第六场

A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 156    Accepted Submission(s): 72 Problem Description There are an equation.∑0≤k1,k2,?km≤n∏1?j<m(kj+1kj)%1000000007=?We define t

HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 79    Accepted Submission(s): 48 Problem Description Two players take turns picking candies from n heaps,the player who picks the las

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分相同,第一部分与第二部分对称. 现在给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法,求出以第i个点为中心的回文串长度,记录到数组p中 要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样. 因为我们已经记录下来以