zoukankan      html  css  js  c++  java
  • CodeForces 754D Fedor and coupons (优先队列)

    题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大。

    析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点,

    之间的距离。优先队列只要存储右端点就好。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <unordered_map>
    #include <unordered_set>
    #define debug() puts("++++");
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1LL << 60;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 3e5 + 5;
    const int mod = 2000;
    const int dr[] = {-1, 1, 0, 0};
    const int dc[] = {0, 0, 1, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct Node{
        int l, r, id;
        bool operator < (const Node &p) const{
            return l < p.l;
        }
    };
    Node a[maxn];
    
    int main(){
        while(scanf("%d %d", &n, &m) == 2){
            for(int i = 0; i < n; ++i){
                scanf("%d %d", &a[i].l, &a[i].r);
                a[i].id = i + 1;
            }
            sort(a, a + n);
            priority_queue<int, vector<int>, greater<int> >pq;
            int ans = 0;
            int t = 0;
            for(int i = 0; i < n; ++i){
                pq.push(a[i].r);
                if(pq.size() > m)  pq.pop();
                int tmp = pq.top() - a[i].l + 1;
                if(pq.size() == m && ans < tmp){
                    ans = tmp;
                    t = a[i].l;
                }
            }
    
            printf("%d
    ", ans);
            if(!ans){
                printf("1");
                for(int i = 2; i <= m; ++i)  printf(" %d", i);
                continue;
            }
            else{
                int cnt = 0;
                for(int i = 0; i < n && m; ++i) if(a[i].l <= t && a[i].r >= t + ans - 1){
                    if(cnt)  putchar(' ');
                    printf("%d", a[i].id);
                    --m;
                    ++cnt;
                }
            }
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    redhat 6.4重新安装python和yum
    【机器学习】置信区间上界算法UCB(Upper Confidence Bound)
    MAC终端zsh配置
    [Android] 重新打包(替换)签名APK
    UNI-APP常用方法
    一个请求的生命周期
    jquery追加元素的几种方法?(包括append()、prepend()、after()、before()、insertAfter()、insertBefore())
    怎样查外键建在哪个表上
    Redis实现分布式锁
    sql语句中对单个字段去重,distinct和group by性能分析
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6287343.html
Copyright © 2011-2022 走看看