Efficient Solutions
64-bit integer IO format: %lld Java class name: Main
Problem I
Efficient Solutions
Input: Standard Input
Output: Standard Output
"Our marriage ceremonies are solemn, sober |
J. Michael Straczynski, "The Deconstruction of Falling Stars."
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 dominatedby 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 grooms after 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 ith update. 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 |
有n个人,每个人都有两个属性x和y,如果对于一个人p(x,y),不存在另一个人(xx,yy),使得xx<x,yy<y或xx<=x,yy<y;wo,就说P是有优势的.每次给出一个人的信息,要求输出只考虑当前已获得的信息的前提下,多少人是有优势的.
暴力的话超时 又是STL容器 nlogn
如果一个人不是优势的 以后他也不是优势的 对后面也没影响
画个二维图按一定顺序排列 会发现 插入一个点只要和自己左下方的点比就可以了
#include<stdio.h>
#include<set>
using namespace std;
struct point
{
int a,b;
friend bool operator < (point aa,point bb)
{
if(aa.a==bb.a) return aa.b<bb.b;
else return aa.a<bb.a;
}
};
multiset<point>ss;
multiset<point>::iterator it;
int main()
{
int T;
scanf("%d",&T);
for(int tt=1; tt<=T; tt++)
{
if(tt>1) printf("\n");
printf("Case #%d:\n",tt);
ss.clear();
int n,a,b;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&a,&b);
point p=(point){a,b};
it=ss.lower_bound(p);//(或等于)
if(it==ss.begin()||(--it)->b>b)//前面条件改成!ss.size()不行
{
ss.insert(p);
it=ss.upper_bound(p);//返回指向大于某值的第一个元素的迭代器
while(it!=ss.end()&&it->b>=b)//进去之后它是优势点会把其他点优势去除
ss.erase(it++);
}
printf("%d\n",ss.size());
}
}
return 0;
}