zoukankan      html  css  js  c++  java
  • 洛谷 [P2859] 摊位预定

    贪心

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    const int MAXN = 50005;
    int init() {
    	int rv = 0, fh = 1;
    	char c = getchar();
    	while(c < '0' || c > '9') {
    		if(c == '-') fh = -1;
    		c = getchar();
    	}
    	while(c >= '0' && c <= '9') {
    		rv = (rv<<1) + (rv<<3) + c - '0';
    		c = getchar();
    	}
    	return fh * rv;
    }
    struct cow{
    	int s, t, num;
    	bool operator < (const cow & b) const {
    		return s < b.s;
    	}
    }a[MAXN];
    struct ket{
    	int num, tim;
    	bool operator < (const ket & b) const {
    		return tim > b.tim;
    	}
    };
    int n, ans[MAXN], cnt;
    priority_queue <ket> q;
    int main() {
    	n = init();
    	for(int i = 1; i <= n; i++) {
    		cin >> a[i].s >> a[i].t;
    		a[i].num = i;
    	}
    	sort(a + 1, a + n + 1);
    	q.push({1, a[1].t});
    	ans[a[1].num] = 1;
    	cnt++;
    	for(int i = 2; i <= n; i++) {
    		if(q.top().tim < a[i].s) {
    			ket t;
    			t = q.top();
    			q.pop();
    			t.tim = a[i].t;
    			q.push(t);
    			ans[a[i].num] = t.num;
    		}else {
    			cnt++;
    			ket t;
    			t.num = cnt;
    			t.tim = a[i].t;
    			q.push(t);
    			ans[a[i].num] = cnt;
    		}
    	}
    	cout << cnt << endl;
    	for(int i = 1;i <= n; i++) {
    		printf("%d
    ", ans[i]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Linux下安装MySql8
    root与alias主要区别
    TCP/UDP 常用端口列表
    kafka文档
    kafka监测工具(可视化)
    Kafka消息队列
    postgresql
    gulp babel 配置不报错也没有输出结果的原因
    gulp中常用的模块
    判断浏览器标签页是隐藏或者显示状态
  • 原文地址:https://www.cnblogs.com/Mr-WolframsMgcBox/p/8617152.html
Copyright © 2011-2022 走看看