Gym - 101810A ACM International Collegiate Programming Contest (2018)

bryce1010模板
http://codeforces.com/gym/101810/problem/A
大模拟,写崩了,代码借队友的。。。。。。

注意处理段与段的连接问题:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
struct node{
    long long l,r,val;
    node(){}
    node(long long ll,long long rr,long long vval)
    {
        l=ll;
        r=rr;
        val=vval;
    }
}ar[maxn];
bool cmp(node a,node b)
{
    return a.l<b.l;
}
int main(){
    int t,m;
    long long k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld",&m,&k);
        int u,v,cost;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&cost);
            ar[i]=node(u,v,cost);
        }
        sort(ar,ar+m,cmp);
        long long sum=0,ans=0;
        int pre=0;
        for(int i=0;i<m;)
        {
            if(ar[i].r-ar[pre].l+1<=k)
            {
                sum+=(ar[i].r-ar[i].l+1)*ar[i].val;
                ans=max(ans,sum);
                i++;
            }
            else
            {
                if(ar[i].l-ar[pre].l>=k)
                {
                    if(ar[i].r-ar[pre].r<=k)
                    {
                        ans=max(ans,sum+(ar[i].r-ar[i].l+1)*ar[i].val-(ar[i].r-ar[pre].l+1-k)*ar[pre].val);
                        sum+=(ar[i].r-ar[i].l+1)*ar[i].val;
                        i++;
                    }
                    else
                    {
                        sum-=(ar[pre].r-ar[pre].l+1)*ar[pre].val;
                        pre++;
                    }
                }
                else
                {
                    ans=max(ans,sum+(k-ar[i].l+ar[pre].l)*ar[i].val);
                    if(ar[i].r-ar[pre].r<k)
                    {
                        sum+=(ar[i].r-ar[i].l+1)*ar[i].val;
                        ans=max(ans,sum-(ar[i].r-ar[pre].l+1-k)*ar[pre].val);
                        i++;
                    }
                    else
                    {
                        sum-=(ar[pre].r-ar[pre].l+1)*ar[pre].val;
                        pre++;
                    }
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
posted @ 2018-07-29 15:39  Bryce1010  阅读(202)  评论(0编辑  收藏  举报