zoukankan      html  css  js  c++  java
  • HDU 5353 Average 贪心

    就是贪心啊,不知道为啥总是不过,总是WA

    方法不对吗?

    将数组扩展一倍,从左到右扫描,大于平均数就给右边的,小于就从右边拿,等于就不变,记录下操作类型。

    大于2直接NO,不知道哪错了,自己出了一些数据也都过了

    路过的大神多瞄一眼

    多校每周只有两场,还是尽量把题全过了吧

      1 #pragma comment(linker, "/STACK:102400000,102400000")
      2 #include <iostream>
      3 #include <cstdio>
      4 #include <fstream>
      5 #include <algorithm>
      6 #include <cmath>
      7 #include <deque>
      8 #include <vector>
      9 #include <queue>
     10 #include <string>
     11 #include <cstring>
     12 #include <map>
     13 #include <stack>
     14 #include <set>
     15 #define LL long long
     16 #define INF 0x3f3f3f3f
     17 #define MAXN 200005
     18 using namespace std;
     19 struct Node
     20 {
     21     int x, y;
     22     Node(int x = 0, int y = 0):x(x), y(y){};
     23 };
     24 vector<Node> res;
     25 int n;
     26 LL sum;
     27 LL a[MAXN];
     28 int f[MAXN];
     29 int main()
     30 {
     31 #ifndef ONLINE_JUDGE
     32     freopen("in.txt", "r", stdin);
     33     //freopen("out.txt", "w", stdout);
     34 #endif // OPEN_FILE
     35    int T;
     36    scanf("%d", &T);
     37    for(int cas = 1; cas <= T; cas++){
     38         scanf("%d", &n);
     39         sum = 0;
     40         for(int i = 1; i <= n; i++){
     41             scanf("%d", &a[i]);
     42             a[i + n] = a[i];
     43             sum += a[i];
     44         }
     45         if(sum % n != 0){
     46             printf("NO
    ");
     47             continue;
     48         }
     49         LL ave = sum / n;
     50         memset(f, 0, sizeof(f));
     51         bool noans = false;
     52         for(int i = 1; i < 2 * n; i++){
     53             if(a[i] == ave){
     54                 continue;
     55             }
     56             if(a[i] < ave){
     57                 if(ave - a[i] > 2){
     58                     noans = true;
     59                     break;
     60                 }
     61                 a[i]++;
     62                 a[i + 1]--;
     63                 f[i] = 1;
     64                 continue;
     65             }
     66             if(a[i] > ave){
     67                 if(a[i] - ave > 2){
     68                     noans = true;
     69                     break;
     70                 }
     71                 a[i]--;
     72                 a[i + 1]++;
     73                 f[i] = -1;
     74             }
     75         }
     76         if(noans){
     77             printf("NO
    ");
     78             continue;
     79         }
     80         noans = true;
     81         int cnt = 1;
     82         int pos = 0;
     83         for(int i = 2; i <= 2 * n; i++){
     84             if(a[i] == a[i - 1]){
     85                 cnt++;
     86             }
     87             else{
     88                 cnt = 1;
     89             }
     90             if(cnt == n){
     91                 noans = false;
     92                 pos = i;
     93                 break;
     94             }
     95         }
     96         if(noans){
     97             printf("NO
    ");
     98             continue;
     99         }
    100         printf("YES
    ");
    101         int x, y;
    102         res.clear();
    103         for(int i = pos - n + 1; i <= pos; i++){
    104             if(f[i] == 0){
    105                 continue;
    106             }
    107             if(f[i] == 1){
    108                 x = i + 1;
    109                 y = i;
    110             }
    111             else{
    112                 x = i;
    113                 y = i + 1;
    114             }
    115             if(x > n){
    116                 x -= n;
    117             }
    118             if(y > n){
    119                 y -= n;
    120             }
    121             res.push_back(Node(x, y));
    122         }
    123         printf("%d
    ", res.size());
    124         for(int i = 0; i < res.size(); i++){
    125             printf("%d %d
    ", res[i].x, res[i].y);
    126         }
    127    }
    128 }
  • 相关阅读:
    Mac nginx 简单文件服务器
    cocos-lua
    Sublime Text 3 快捷键精华版
    VS中dumpbin工具的使用
    【2022新教程】Ubuntu server 20.04如何安装nvidia驱动和cuda-解决服务器ssh一段时间后连不上的问题
    linux如何将一个正在运行的进程转入到后台执行且中断shh连接不被kill掉
    Ubuntu如何修改默认python版本-sudo和不用sudopython不一致怎么办
    Ubuntu中virtual enviroment在sudo下无效怎么办?
    时间复杂度为O(1)的抽样算法——别名采样(alias sample method)
    Luke技术小站公告板
  • 原文地址:https://www.cnblogs.com/macinchang/p/4712786.html
Copyright © 2011-2022 走看看