牛客小白月赛2 I 艺 (离散化)

题目链接:https://www.nowcoder.com/acm/contest/86/I
分析
很容易想到用离散化做,只要把时间离散化了,然后判断一下每个时间段看哪个电视节目好就行了。用双指针维护当前时间播放的电视节目。 一些细节的处理我直接边敲边根据样例调整的,最后WA,发现竟然是某个时间段可以不看电视,改了后就AC了。
代码:

#include <iostream>
#include<map>
#include<algorithm>
#include<set>
#include<cstdio>
using namespace std;
int N,M,T;
struct G{int x,v;};
bool cmp(G a,G b){return a.x<b.x;}
const int maxn=1e5+10;
G a[maxn],b[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin>>N>>M>>T;
    for(int i=0;i<N;i++)cin>>a[i].x>>a[i].v;
    for(int i=0;i<M;i++)cin>>b[i].x>>b[i].v;
    sort(a,a+N,cmp);
    sort(b,b+M,cmp);
    set<int>myset;
    a[N].v=a[N-1].v,a[N].x=T;
    b[M].v=b[M-1].v,b[M].x=T;
    N++,M++;
    for(int i=0;i<N;i++)myset.insert(a[i].x);
    for(int i=0;i<M;i++)myset.insert(b[i].x);

    set<int>::iterator it=myset.begin();

    int t=0,nex_t;
    long long ans=0;
    for(int i=0,j=0;t<T;){
        while((i+1<N&&a[i+1].x<=t))i++;
        while((j+1<M&&b[j+1].x<=t))j++;
        int tem=max(a[i].v,b[j].v);
        tem=max(tem,0);
        it++;
        nex_t=(*it);
        //printf("%d to %d: %d\n",t,nex_t,tem);
        ans+=tem*(nex_t-t);
        t=nex_t;
    }
    cout<<ans<<endl;


    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Surrender/p/8969942.html