Codeforces 497C Distributing Parts set+贪心

题目链接:点击打开链接

题意:

给定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 ll
const 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 int
int 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;
}
时间: 2024-08-08 11:00:36

Codeforces 497C Distributing Parts set+贪心的相关文章

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1

codeforces 343C Read Time 二分 + 贪心

http://codeforces.com/problemset/problem/343/C 题意: 有一些磁头,给出了起始的位置,给出了一些磁盘中需要访问的地点.求这些磁头移动的最小的次数. 思路: 二分找出满足要求的最小的时间,对于当前尝试的时间进行贪心判断是否可用.对于贪心,因为每一个磁头都需要读到还没有人读过的最左边的地方.如果这个都不能满足,那么这个时间是不可以的. 在这基础上,对于同样的这么多时间,尽可能的让这个磁头读到能读到的最右边,这样就可以将还没有读过的地方尽可能的往右推. 如

Codeforces 402D Upgrading Array:贪心 + 数学

题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中p是s的最小质因子: f(1) = 0 f(s) = f(s/p) + 1 (p不是坏质数) f(s) = f(s/p) - 1 (p是坏质数) 你可以任意次数地进行操作:给a[1 to i]的每个数都除以gcd(a[1 to i]). 问你 ∑ f(a[i)最大为多少. 题解: 函数f(s)的实际

Codeforces 1119E Pavel and Triangles (贪心)

Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal to powers of two. He has \(a_0\) sticks of length \(2^0=1\), \(a1\) sticks of length \(2^1=2\), ..., \(a_{n?1}\) sticks of length \(2^{n?1}\). Pavel wa

Codeforces 424 B Megacity【贪心】

题意:给出城市(0,0),给出n个坐标,起始人数s,每个坐标k个人, 每个坐标可以覆盖到半径为r的区域,r=sqrt(x*x+y*y)的区域,问最小的半径是多少,使得城市的总人数大于等于1000000 最开始是排序,贪心来做的,发现sqrt的精度老达不到要求,于是翻了代码 于是发现用map就可以解决了 map<int,int>,it->first是第一个int的内容,it->second是第二个int的内容 话说本来是按照标签来找的,想做二分查找的题目的= = 1 #include