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\n", 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\n", maxx);
    printf("%d %d\n", 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\n", size);
    for (int i = 0; i < ans[pos].size(); i++) {
        printf("%d %d\n", ans[pos][i].l, ans[pos][i].r);
    }
    return 0;
}

 

posted @ 2019-03-21 10:38  Albert_liu  阅读(196)  评论(0编辑  收藏  举报