zoukankan      html  css  js  c++  java
  • Codeforces 1141F2(贪心、预处理)

    要点

    • 一开始dp然后码力太辣鸡并且算法带假于是调了很久一交还WA在28……
    • 吐槽完毕。后来想拿栈优化dp时发现其实完全不需要dp,贪心选取即可,当前的不兼容就干脆不要它了,结果不会变差。然后想要什么就预处理什么即可。
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <unordered_map>
    using namespace std;
    
    const int maxn = 1505;
    int n, a[maxn], sum[maxn], ans, val, ID;
    int d[maxn * maxn], cnt;
    vector<int> v[maxn * maxn];
    vector<int> vv[maxn];
    unordered_map<int, int> mp, L[maxn];
    
    int main() {   
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i];
        for (int i = 1; i <= n; i++) {
            for (int j = i - 1; ~j; j--) {
                int t = sum[i] - sum[j];
                d[++cnt] = t;
                if (!L[i][t])   L[i][t] = j + 1, vv[i].push_back(t);
            }
        }
    
        sort(d + 1, d + 1 + cnt);
        cnt = unique(d + 1, d + 1 + cnt) - d - 1;
        for (int i = 1; i <= cnt; i++)  mp[d[i]] = i; 
            
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < vv[i].size(); j++) {
                int t = vv[i][j], id = mp[t];
                if (v[id].empty() || v[id].back() < L[i][t]) {
                    v[id].push_back(i);
                }
                if (ans < v[id].size()) {
                    ans = v[id].size();
                    val = t;
                    ID = id;
                }
            }
        }
    
        printf("%d
    ", ans);
        for (int i : v[ID]) {
            printf("%d %d
    ", L[i][val], i);
        }
        return 0;
    }
    
  • 相关阅读:
    LeetCode——Basic Calculator
    LeetCode——Sqrt(x)
    LeetCode——Binary Search Tree Iterator
    LeetCode——Search for a Range
    LeetCode——pow(x, n)
    LeetCode——Single Number II
    LeetCode——Summary Ranges
    LeetCode——Largest Number
    LeetCode——Kth Largest Element in an Array
    LeetCode——Implement Stack using Queues
  • 原文地址:https://www.cnblogs.com/AlphaWA/p/10897140.html
Copyright © 2011-2022 走看看