码迷,mamicode.com
首页 > 其他好文 > 详细

[JZOJ 5861] 失意

时间:2018-09-10 13:40:17      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:while   dig   ret   failure   char   out   printf   get   push   

思路:
求交集最大老套路,排序之后用堆维护即可。

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 1000010;
inline int read() {
    int q=0,f=1;char ch = getchar();
    while(!isdigit(ch)){
        if(ch==‘-‘)f=-1;ch=getchar();
    }
    while(isdigit(ch)){
        q=q*10+ch-‘0‘;ch=getchar();
    }
    return q*f;
}
struct lne {
    int l,r,id;
    bool operator < (lne x) const{
        return r > x.r;
    }
}l[maxn];
int ans;
int n,m;
priority_queue<lne> q;
bitset<maxn>vis,p;
int siz;
inline bool cmp(lne a,lne b) {
    return (a.l == b.l) ? (a.r > b.r) : (a.l < b.l);
}
int main () {
    freopen("failure.in","r",stdin);
    freopen("failure.out","w",stdout);
    read();
    n = read(),m = read();
    for(int i = 1;i <= n; ++i) {
        l[i].l = read();
        l[i].r = read();
        l[i].id = i;
    }
    sort(l + 1,l + n + 1,cmp);
    for(int i = 1;i <= m; ++i) {
        q.push(l[i]);
        ++siz;
        vis[l[i].id] = 1;
    }
    lne top = q.top();
    ans = max(top.r - l[m].l,0);
    p = vis;
    for(int i = m + 1;i <= n; ++i) {
        q.push(l[i]);
        ++siz;
        vis[l[i].id] = 1;
        if(siz > m) {
            top = q.top();
            vis[top.id] = 0;
            q.pop();
            siz--;
        }
        if(siz == m) {
            top = q.top();
            int len = max(top.r - l[i].l,0);
            if(len >= ans) {
                ans = len;
                p = vis;
            }
        }
    }
    printf("%d\n",ans);
    for(int i = 1;i <= n; ++i) {
        if(p[i]) {
            printf("%d ",i);
        }
    }
    return 0;
}

[JZOJ 5861] 失意

标签:while   dig   ret   failure   char   out   printf   get   push   

原文地址:https://www.cnblogs.com/akoasm/p/9618366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!