Efficient Solutions+BST(排序二叉树)

Efficient Solutions
Input: Standard Input 
Output: Standard Output
"Our marriage ceremonies are solemn, sober 
moments of reflection; also regret, disagreement, 
argument and mutual recrimination. Once you know
it can't get any worse, you can relax and enjoy 
the marriage." 
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 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 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 


100 200 

100 200 
101 202 

100 200 
200 100 

11 20 
20 10 
20 10 
100 20 
1 1 
Case #1: 

Case #2: 


Case #3: 


Case #4: 





解决方案: 人是只增不减的,所以现在有优势的人,可能会失势,一旦失势就永远不会优势,所以可以动态的维护优势人群。首先应该知道优势人群的坐标是怎样的,第一不会有两个点的横坐标或纵坐标相同,其次对于任意的A、B两个点,若A点在B点的左边,那么A点一定在B点的上边。这样从左向右看,点的横纵坐标不断严格减小。

那么当添加一个点是什么情况的呢?

有两种情况:1)若该点不占优势,只需判断其左边有相邻的点的y轴坐标比它小,那么此点可以忽略。

2)若该点有优势,加入点集,则可能会有一些点失势,即该点的正上,右上,正右方的点都会失势。

这里可采用multiset(看重集)来表示这个点集,第一种情况比较lower_bound(p)和p的y坐标,第二重情况从upper_bound(p)开始删除所有失势的点。

code:

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
struct node{
  int a,b;
  node(int A,int B){a=A;b=B;}
  bool operator <(const node &s)const{

  return a<s.a||(a==s.a&&s.b<b);
  }

};
multiset<node>M;
multiset<node>::iterator it;
int main()
{
    int t,k=0;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        M.clear();
        printf("Case #%d:\n",++k);
        for(int i=0;i<n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        node S(a,b);
        it=M.lower_bound(S);
        if(it==M.begin()||(--it)->b>b){
            M.insert(S);
            it=M.upper_bound(S);
            while(it!=M.end()&&it->b>=b) M.erase(it++);
        }
        printf("%d\n",M.size());
        }
        if(t)
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值