zoukankan      html  css  js  c++  java
  • Codeforces Round #547 (Div. 3) F1&F2 Same Sum Blocks

    
    

     越发的菜了,WA了还找不出bug在哪。

    其实只需要做一下离散化,然后贪心的判断一下是否需要加入答案就可以了,顺便记录一个最大值。

    #include "bits/stdc++.h"
    
    using namespace std;
    const int maxn = 1000;
    int n;
    int d[maxn];
    int out = 0;
    
    int Find(int from, int val) {
        int cnt = 0;
        int l = from, r = from;
        while (r <= n) {
            while (d[r] - d[l - 1] > val && l < r)l++;//这里会有错误的跳过,比如当数据为 4 7 5 -8 时,不能找到(2,4)这个区间
            if (d[r] - d[l - 1] == val) {
                cnt++;
                if (out)
                    printf("%d %d
    ", l, r);
                l = r + 1;
            }
            r++;
        }
        return cnt;
    }
    
    int main() {
        scanf("%d", &n);
        int x;
        for (int i = 1; i <= n; i++) {
            cin >> x;
            d[i] = d[i - 1] + x;
        }
        int ansr, ansl, maxx = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = i; j <= n; j++) {
                int temp = Find(j + 1, d[j] - d[i - 1]) + 1;
                if (temp > maxx) {
                    maxx = temp;
                    ansr = j;
                    ansl = i;
                }
            }
        }
        printf("%d
    ", maxx);
        printf("%d %d
    ", ansl, ansr);
        out = 1;
        Find(ansr + 1, d[ansr] - d[ansl - 1]);
    }
    WA的代码
    #include "bits/stdc++.h"
    
    using namespace std;
    const int maxn = 1510 * 1510;
    int n;
    int d[maxn];
    struct node {
        int l, r;
    } te;
    vector<node> ans[maxn];
    map<int, int> mp;
    int tot = 0;
    
    int main() {
        cin >> n;
        int x, val;
        int pos = 0, size = 0;
        for (int i = 1; i <= n; i++) {
            cin >> x;
            d[i] = d[i - 1] + x;
            for (int j = 1; j <= i; j++) {
                val = d[i] - d[j - 1];
                te.l = j, te.r = i;
                if (!mp.count(val)) {
                    mp[val] = ++tot;
                }
                val = mp[val];
                if (!ans[val].size()) {
                    ans[val].push_back(te);
                } else if (ans[val][ans[val].size() - 1].r < te.l) {
                    ans[val].push_back(te);
                }
                if (ans[val].size() > size) {
                    size = ans[val].size();
                    pos = val;
                }
            }
        }
        printf("%d
    ", size);
        for (int i = 0; i < ans[pos].size(); i++) {
            printf("%d %d
    ", ans[pos][i].l, ans[pos][i].r);
        }
        return 0;
    }
  • 相关阅读:
    系统设计实现
    反编译
    android layout analyze
    2016.7.20-notes
    如何认识刘昊然?
    leetcode-中等-队列-二叉树的层次遍历
    leetcode-简单-栈-逆波兰表达式
    leetcode-简单-栈-有效的括号
    队列-链式队列
    队列-循环队列-顺序存储
  • 原文地址:https://www.cnblogs.com/albert-biu/p/10570028.html
Copyright © 2011-2022 走看看