zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 61 (Rated for Div. 2) C

      题意是给q条线段,然后取q-2条,问最多能覆盖多少个点?

      可以换个角度看成是舍弃掉2条线段后,最多覆盖多少个点。暴力枚举舍弃的2条线段,然后求舍弃后的值,然后取最大值。关键    就是要o(1)求出舍弃掉2条线段后的被覆盖点数,可以预处理出覆盖一次的点数量和覆盖2次的点数量的前缀和,,然后根据2条线段是具体位置关系进行操作,假如两个线段没有重叠部分的话,只需要总覆盖点数分别减去两条线段内被覆盖一次的点的数量即可,若有重叠还要减去重叠区域被覆盖2次的点的数量。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=5005;
    pair<int,int>s[maxn];
    int cnt[maxn],pre[3][maxn];
    
    int main()
    {
        int ans=-1,n,q,tmp=0;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=q;i++)
            scanf("%d%d",&s[i].first,&s[i].second),cnt[s[i].first]++,cnt[s[i].second+1]--;
        for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
        for(int i=1;i<=n;i++)
        {
            if(cnt[i]) tmp++;
            pre[1][i]=pre[1][i-1];
            pre[2][i]=pre[2][i-1];
            if(cnt[i]==1)
                pre[1][i]++;
            else
                if(cnt[i]==2)
                    pre[2][i]++;
        }
        //cout<<tmp<<endl;
        sort(s+1,s+q+1);
        //for(int i=1;i<=q;i++) cout<<s[i].first<<"   "<<s[i].second<<endl;
        for(int i=1;i<=q;i++)
            for(int j=i+1;j<=q;j++)
            {
                if(s[j].first>s[i].second)
                    ans=max(ans,tmp-(pre[1][s[i].second]-pre[1][s[i].first-1])-(pre[1][s[j].second]-pre[1][s[j].first-1]));
                else
                    if(s[j].second>=s[i].second)
                    {
                        ans=max(ans,tmp-(pre[1][s[j].first-1]-pre[1][s[i].first-1])-(pre[1][s[j].second]-pre[1][s[i].second])-(pre[2][s[i].second]-pre[2][s[j].first-1]));
                    }
                    else
                    {
                        ans=max(ans,tmp-(pre[1][s[j].first-1]-pre[1][s[i].first-1])-(pre[1][s[i].second]-pre[1][s[j].second])-(pre[2][s[j].second]-pre[2][s[j].first-1]));
                    }
    
            }
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    hdu 6702 ^&^ 位运算
    hdu 6709 Fishing Master 贪心
    hdu 6704 K-th occurrence 二分 ST表 后缀数组 主席树
    hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列 LCIS
    hdu 5909 Tree Cutting FWT
    luogu P1588 丢失的牛 宽搜
    luogu P1003 铺地毯
    luogu P1104 生日
    luogu P1094 纪念品分组
    luogu P1093 奖学金
  • 原文地址:https://www.cnblogs.com/eason9906/p/11754803.html
Copyright © 2011-2022 走看看