UVA11020 Efficient Solutions

来源:互联网 发布:帝国cms模板目录 编辑:程序博客网 时间:2024/04/30 02:57

The princess of Centauri Prime is the galaxy's most eligible bachelorette of the year. She has hopeful grooms lined up in front of the royal palace for a chance to spend 5 minutes to try and impress her. After 5 minutes, the gentleman is carried out of the royal chambers by the palace guards, and the princess makes a decision. She rates the lad on his lineage and charm by giving him a score for each of the two properties. On Centauri Prime, low scores are better than high scores.

Suppose that she observes two gentlemen - A and B. She assigns A the scores LA and CA (for lineage and charm, respectively). B receives scores LB and CB. Then A is dominated by B if either

  • LB < LA and CB <= CA, or
  • LB <= LA and CB < CA.

In other words, if at least one of B's scores is better than A's, and the other score is not worse. She considers a gentleman to be efficient (or Pareto-optimal) if she has not yet met any other gentleman who dominates him. She maintains a list of efficient grooms and updates it after each 5-minute presentation.

Given the queue of bachelors and the scores assigned to them by the princess, determine the number of entries in the list of efficient groomsafter each performance.

Input
The first line of input gives the number of cases, N (0<N<40)N test cases follow.

Each one starts with a line containing n (0≤n≤15000) - the size of the queue. The next n lines will each contain two scores (integers in the range [0, 109]). Initially, the list is empty.

Output
For each test case, output one line containing "Case #x:" followed by n lines, line i containing the size of the list of efficient grooms after the ithupdate. Print an empty line between test cases.

 

Sample Input

Sample Output

4
1
100 200
2
100 200
101 202
2
100 200
200 100
5
11 20
20 10
20 10
100 20
1 1
Case #1:
1
 
Case #2:
1
1
 
Case #3:
1
2
 
Case #4:
1
2
3
3
1

转载请注明出处:http://blog.csdn.net/scut_pein/article/details/19841869

题意:有n个人,每个人有两个属性x和y,如果对于一个人P(x,y),不存在另外一个人(x1,y1),使得x1 < x,y1 <= y 或者 x1 <= x,y1 < y,我们说P是有优势的,每次给出一个人的信息,要求输出只考虑当前已经获得的信息的前提下,多少人是有优势的。

思路:刚开始以为只要输出多少人是有优势的。如果问题是这样很简单,只要先存到点,然后排序。每个点和上一个点(P)比较,如果失去优势,则保持不变。如果该点能保持优势,则P变为该点,ans++。

           现在问题是要每读一个点,就要输出现在的ans。用multiset实现。二分找到插入处,如果该点有优势,即要被插入集合,那么该点后面的点要判断是否失去优势。直到找到一个没失去优势的或者集合尾。如果该点没有优势,不需要插入,也不会有后面的点因为这个点失去优势,因为让这个点失去优势的点必然也会让这个点后面的点失去优势。。。讲的好繁琐%>_<%、、、、

#include <iostream>#include <cstdio>#include <cstring>#include <set>using namespace std;struct Point{int x,y;bool operator < (const Point & a) const{return x < a.x || (x == a.x && y < a.y);}};multiset <Point> S;multiset <Point>::iterator it;int main(){int t;scanf("%d",&t);for(int cas = 1;cas <= t;cas++){if(cas > 1)printf("\n");printf("Case #%d:\n",cas);int n,x,y;scanf("%d",&n);S.clear();while(n--){scanf("%d%d",&x,&y);Point p;p.x = x;p.y = y;it = S.lower_bound(p);if(it == S.begin() || (--it) -> y > y){S.insert(p);it = S.upper_bound(p);while(it != S.end() && it -> y >= y)S.erase(it++);}printf("%d\n",S.size());}}return 0;}





0 0