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;
    }
    
  • 相关阅读:
    angular.js 渲染
    HTML5 与 CSS3 jQuery部分知识总结
    下拉滚动协议文本框展示样式(不可删除文本内容)
    06对象
    05数组
    1文字与字体
    04函数
    03循环
    02运算符
    01基础
  • 原文地址:https://www.cnblogs.com/Mr-WolframsMgcBox/p/8617152.html
Copyright © 2011-2022 走看看