Codeforces 497C Distributing Parts set+貪心

來源:互聯網
上載者:User

標籤:blog   http   ar   sp   for   on   2014   art   log   

題目連結:點擊開啟連結

題意:

給定n個任務

下面[l, r]是n個任務需要佔用的時間。

m個人

下面是m個人的空閑時間以及這個人至多能做的任務個數(一個人同一時刻只能做一個任務,即人是單線程的)

[l, r] num

問:

若任務不能被全部完成則輸出NO

否則輸出YES

輸出每個任務是誰完成的。

思路:

把人和任務放一起按右端點排序。

若遇到了任務則把任務的左端點放到set裡。

若遇到了人,則把set中>=人的左端點的 num個數刪掉。

c:

#include <bits/stdc++.h>template <class T>inline bool rd(T &ret) {    char c; int sgn;    if(c=getchar(),c==EOF) return 0;    while(c!='-'&&(c<'0'||c>'9')) c=getchar();    sgn=(c=='-')?-1:1;    ret=(c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');    ret*=sgn;    return 1;}template <class T>inline void pt(T x) {    if (x <0) {        putchar('-');        x = -x;    }    if(x>9) pt(x/10);    putchar(x%10+'0');}using namespace std;typedef long long ll;#define int llconst int N = 200050;int n, m;struct node{    int op, l, r, num, id;}a[N*2];bool cmp(node x, node y){    if(x.r != y.r)        return x.r<y.r;    if(x.op!=y.op)        return x.op<y.op;}int top, b[N];void input(){    top = 0;    for(int i = 1; i <= n; i++){        rd(a[top].l); rd(a[top].r);        a[top].op = 0;        a[top].id = i;        top++;    }    rd(m);    for(int i = 1; i <= m; i++){        rd(a[top].l); rd(a[top].r); rd(a[top].num);        a[top].op = 1;        a[top].id = i;        top++;    }    sort(a, a+top, cmp);}struct Edge{    int id, l;    bool operator<(const Edge&e)const{        if(e.l!=l)return e.l>l;        return e.id>id;    }    Edge(int a=0,int b=0):id(a),l(b){}}tmp;set<Edge>s;set<Edge>::iterator p;#undef intint main(){#define int ll    while(cin>>n){        input();        int ans = 0;        s.clear();        for(int i = 0; i < top; i++){            if(a[i].op){                while(s.size() && a[i].num--)                {                    p = s.lower_bound(Edge(0, a[i].l));                    if(p == s.end())break;                    tmp = *p;                    b[tmp.id] = a[i].id;                    s.erase(p);                    ans++;                }            }            else {                s.insert(Edge(a[i].id, a[i].l));            }        }        if(ans == n)        {            puts("YES");            for(int i = 1; i <= n; i++){pt(b[i]); putchar(' ');}        }        else puts("NO");    }    return 0;}


Codeforces 497C Distributing Parts set+貪心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.