码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3190 Stall Reservations

时间:2017-01-20 00:01:38      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:pos   重载   code   合并   org   并且   problem   分配   个数   

http://poj.org/problem?id=3190

//最初思路 按数组排序 也好 堆维护也好 都是想 让开始时间 > 结束时间的牛合并 然后最后求出 个数就好
//但是这样无法求得每头牛的分配过程
//所以 模拟 加贪心 堆模拟正在挤奶的机器 按结束时间从小到大排列 没搜索加入一头牛 就结束一头牛的挤奶
//这样可以求得每头牛 在哪个机器挤奶 并且如果 新加入牛时 这头牛已经结束 那么它就可以接着使用 否则 只能新加一台机器
//然后stall就是 进一头牛 确定一个的stall 这样就是纯模拟这个个过程
//一道贪心、模拟的好题

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <queue>
 5 
 6 using namespace std;
 7 
 8 int N;
 9 int stall[50003];
10 struct Section
11 {
12     int x,y;
13     int pos;
14     bool operator < (Section a) const //堆按结束时间升序排列
15     {
16         if (y == a.y) return x > a.x;
17         else return y > a.y;
18     }
19 
20 }section[50003];
21 
22 bool cmp(Section a, Section b)//数组按开始时间升序排列
23 {
24     if(a.x == b.x) return a.y < b.y;
25     else return a.x < b.x;
26 }
27 
28 priority_queue<Section> que;
29 int solve()
30 {
31     int ans = 0;
32     Section node;
33     for (int i = 0; i < N;i ++)
34     {
35         if ( !que.empty() )
36         {
37             node = que.top();
38             if (que.top().y < section[i].x)//如果加进这个section的时候有结束的奶牛-->>>这里错了 不是当结束时间和开始时间相等时不能接替的
39             {
40                 stall[section[i].pos] = stall[que.top().pos];
41                 que.pop();//这头奶牛就可以出去了
42             }
43             else
44             {
45                 ans++;
46                 stall[section[i].pos] = ans;
47             }
48         }
49         else
50         {
51             ans++;
52             stall[section[i].pos] = ans;
53         }
54         que.push(section[i]);
55     }
56     return ans;
57 }
58 
59 int main()
60 {
61     freopen("in.txt", "r", stdin);
62     while (~scanf("%d", &N))
63     {
64         while (!que.empty()) que.pop();
65         for (int i = 0; i < N; i++)
66         {
67             scanf("%d%d", &section[i].x, &section[i].y);
68             section[i].pos = i;
69             //stall[section[i].pos] = i;//安排最初的stall
70         }
71         sort(section, section+N, cmp);//当自定义比较函数时 是可以忽略 结构体内部的操作符重载的
72         int ans = solve();
73         printf("%d\n",ans);
74         for (int i = 0;i < N; i++)
75         {
76             printf("%d\n", stall[i]);
77         }
78     }
79     return 0;
80 }

 

POJ 3190 Stall Reservations

标签:pos   重载   code   合并   org   并且   problem   分配   个数   

原文地址:http://www.cnblogs.com/oscar-cnblogs/p/6308993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!