思维/构造 HDOJ 5353 Average

题目传送门

  1 /*
  2     思维/构造:赛后补的,当时觉得3题可以交差了,没想到这题也是可以做的。一看到这题就想到了UVA_11300(求最小交换数)
  3             这题是简化版,只要判断行不行和行的方案就可以了,做法是枚举x[1],x[n]的所有可能,x[2~n-1]能递推出来
  4             x[i]表示i给i+1的值(0/-1/1) 那么 a[i] - x[i] + x[i-1] == ave,详细看代码
  5 */
  6 /************************************************
  7 * Author        :Running_Time
  8 * Created Time  :2015-8-7 8:51:48
  9 * File Name     :A.cpp
 10  ************************************************/
 11
 12 #include <cstdio>
 13 #include <algorithm>
 14 #include <iostream>
 15 #include <sstream>
 16 #include <cstring>
 17 #include <cmath>
 18 #include <string>
 19 #include <vector>
 20 #include <queue>
 21 #include <deque>
 22 #include <stack>
 23 #include <list>
 24 #include <map>
 25 #include <set>
 26 #include <bitset>
 27 #include <cstdlib>
 28 #include <ctime>
 29 using namespace std;
 30
 31 #define lson l, mid, rt << 1
 32 #define rson mid + 1, r, rt << 1 | 1
 33 typedef long long ll;
 34 const int MAXN = 1e5 + 10;
 35 const int INF = 0x3f3f3f3f;
 36 const int MOD = 1e9 + 7;
 37 int a[MAXN];
 38 int x[MAXN];
 39 int n, tot;
 40 ll ave;
 41
 42 bool work(void) {
 43     tot = 0;
 44     if (x[1] != 0)  tot++;
 45     if (x[n] != 0)  tot++;
 46     if (x[n-1] != 0)    tot++;
 47     for (int i=2; i<=n-2; ++i)  {
 48         if (abs (a[i] - ave + x[i-1]) > 1)  return false;
 49         x[i] = a[i] - ave + x[i-1];
 50         if (x[i] != 0)  tot++;
 51     }
 52     if (a[n-1] - x[n-1] + x[n-2] != ave)    return false;
 53     return true;
 54 }
 55
 56 bool judge(void)    {
 57     for (int i=-1; i<=1; ++i)   {
 58         for (int j=-1; j<=1; ++j)   {
 59             x[1] = i, x[n] = j;
 60             if (abs (ave - a[n] + x[n]) <= 1)   {
 61                 x[n-1] = ave - a[n] + x[n];
 62                 if (work ())    return true;
 63             }
 64         }
 65     }
 66     return false;
 67 }
 68
 69 int main(void)    {     //HDOJ 5353 Average
 70     int T;  scanf ("%d", &T);
 71     while (T--) {
 72         scanf ("%d", &n);   ll sum = 0;
 73         for (int i=1; i<=n; ++i)    {
 74             scanf ("%d", &a[i]);    sum += a[i];
 75         }
 76         if (sum % n != 0)   {
 77             puts ("NO");    continue;
 78         }
 79         ave = sum / n;   bool flag = true, same = true;
 80         for (int i=1; i<=n; ++i)    {
 81             if (a[i] < ave - 2 || a[i] > ave + 2)   {
 82                 flag = false;  break;
 83             }
 84             if (a[i] != ave)   same = false;
 85         }
 86         if (!flag)   {
 87             puts ("NO");    continue;
 88         }
 89         if (same)   {
 90             printf ("YES\n0\n");    continue;
 91         }
 92
 93         memset (x, 0, sizeof (x));
 94         if (!judge ())  {
 95             puts ("NO");    continue;
 96         }
 97         puts ("YES");   printf ("%d\n", tot);
 98         for (int i=1; i<=n; ++i)    {
 99             if (x[i] == 0)  continue;
100             else if (x[i] == 1) printf ("%d %d\n", i, (i == n) ? 1 : i + 1);
101             else    printf ("%d %d\n", (i == n) ? 1 : i + 1, i);
102         }
103     }
104
105     return 0;
106 }
时间: 2024-08-13 08:42:53

思维/构造 HDOJ 5353 Average的相关文章

HDOJ 5353 Average 模拟

各种情况特判,然后枚举前两个点之间的关系 Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1723    Accepted Submission(s): 438 Special Judge Problem Description There are n soda sitting around a round tabl

hdu4671 思维构造

pid=4671">http://acm.hdu.edu.cn/showproblem.php? pid=4671 Problem Description Makomuno has N servers and M databases. All databases are synchronized among all servers and each database has a ordered list denotes the priority of servers to access.

构造 HDOJ 5414 CRB and String

题目传送门 题意:给两个字符串s,t,可以在s字符串任意位置后面插入字符c(与前面的不同),问是否能够将s转换为t字符串 构造:首先lens > lent 或者 s[1] != t[1] 一定是No,然后t最前面相同字符长度的部分在s中要相同,否则不能插入,之后的部分只要相同的部分全部存在,不同的部分可以随便插 /************************************************* Author        :Running_Time* Created Time

构造 HDOJ 5399 Too Simple

题目传送门 /* 题意:首先我是懂了的,然后我觉得很难讲清楚就懒得写了,关键理解f1(f2(fm(i)))=i,不懂的戳这里 构造:如果fi(j)不是一一映射到(1~n),重复或者不在范围内的肯定无解,还有没有-1的情况,模拟一下若不能满足f1(f2(fm(i)))=i 也是不行的.除此之外,那么有k个-1,那么方案数是(n!) ^ (k - 1),因为k-1个可以随便排列,最后一个由于之前的确定 */ /*********************************************

#381 Div2 Problem C Alyona and mex (思维 &amp;&amp; 构造)

题意 : 题目的要求是构造出一个长度为 n 的数列, 构造条件是在接下来给出的 m 个子区间中, 要求每一个子区间的mex值最大, 然后在这 m 个子区间产生的mex值中取最小的输出, 并且输出构造出来的序列, 一个mex值的定义是这个区间没有出现过的最小的正整数, 例如(0, 2, 3)的mex = 1    (0, 1, 2)的mex=3 分析 : 发现在这m个mex值中, 最小的肯定是区间长度最小的, 而且这个mex值肯定是等于这个区间的长度, 记这个mex为Min(mex)所以输出的就是

HDU 5353 Average(平分值,求步聚)多校6

Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1948    Accepted Submission(s): 495 Special Judge Problem Description There are n soda sitting around a round table. soda are numbered

HDU 5353—— Average——————【贪心+枚举】

Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2069    Accepted Submission(s): 517Special Judge Problem Description There are n soda sitting around a round table. soda are numbered fr

ACM-ICPC 2018 青岛赛区现场赛 D. Magic Multiplication &amp;&amp; ZOJ 4061 (思维+构造)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061 题意:定义一个长度为 n 的序列 a1,a2,..,an 和长度为 m 的序列 b1,b2,..,bm 所构成的新序列 c 为 a1b1,a1b2,....,anbm,给出最终的序列和两个初始序列的长度,构造出字典序最小的初始序列. 题解:首先我们知道两个个位数相乘最多可以得到两位数,易知最终序列的第一个数字 c1 的构造一定有 a1 的参与,当 a1 <

HDU 5353 Average

题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路:明显每个人最多只能多于平均值2个糖果,因为他只能分别往左和右边的人给1颗.而多于平均值1的人可以任意选1个方向,只要到最后所有人满足了即可.多余糖果超过3的.平均数是浮点型的都是无解. 在第i和第i+1个人之间建两条边(即无向边拆成2条有向边),分别从一方指向另一方.1和n也建两条.分两步: (1)将持有2个多余糖果的人先处理,用D