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;
    }
  • 相关阅读:
    static和final
    java面向对象白话解说
    方法
    数组
    JDK的安装和java程序的开发步骤以及环境变量配置
    VS2010 根据模型生成数据库 打开edmx.sql文件时 vs出现无响应的解决方案
    js简易写法
    .NET程序性能优化基本要领
    数据采集类
    ASP.NET MVC 3 配置EF自动生成模型
  • 原文地址:https://www.cnblogs.com/albert-biu/p/10570028.html
Copyright © 2011-2022 走看看