【codeforces 749 D】【set+二分查找+思路清晰】

传送门:http://codeforces.com/contest/749/problem/D

题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去,问对于每一个询问减掉num个人后是谁赢了拍卖,最小的价格是多少。


思路:用set<pair<int, int> >pos维护竞价人的下标和最大值,把最大值作为pair里面的第一关键字(因为set自带排序功能),vector<int>lst[N]维护每个竞价人的所有竞价(从小到大)

每次询问,先从pos中删去k个,这个时候有三种情况

1)pos为空,输出0 0

2)pos有一个,输出这唯一一个竞价人的最低报价

3)pos个数大于等于2,找出pos里面第一大竞价人和第二大竞价人,然后在第一大竞价人里找刚好大于第二大竞价人的最大竞价值的值就可以了

可以参考官方题解:http://codeforces.com/blog/entry/49186


代码:

#include <bits/stdc++.h>
using  namespace  std;
const int N=2e5+10;

vector<int>lst[N];
set<pair<int, int> >pos;
int n, del[N], mx[N];
bool vis[N];

int  main(){
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        int x, y;
        scanf("%d%d", &x, &y);
        lst[x].push_back(y);
        vis[x]=1;
        mx[x]=max(mx[x], y);
    }
    for(int i=1; i<=n; i++)
        if(vis[i])pos.insert({mx[i], i});

    int q;
    scanf("%d", &q);
    while(q--){
        int k; scanf("%d", &k);
        for(int i=1; i<=k; i++){
            scanf("%d", &del[i]);
            if(vis[del[i]]) pos.erase({mx[del[i]], del[i]});
        }
        int sz=pos.size();
        if(sz==0){
            printf("0 0\n");
        }
        else if(sz==1){
            printf("%d %d\n", pos.begin()->second, *lst[pos.begin()->second].begin());
        }
        else{
            auto it1=pos.end(), it2=pos.end();  it1--; it1--; it2--;
            int tmpnum=it1->first;
            int tmppos=it2->second;  //这个地址下的第二个值
            auto it=upper_bound(lst[tmppos].begin(), lst[tmppos].end(), tmpnum);
            printf("%d %d\n", tmppos, *it);
        }
        for(int i=1; i<=k; i++)if(vis[del[i]])
            pos.insert({mx[del[i]], del[i]});
    }
    return 0;
}

PS:set不熟练,搞得指针、地址、值乱飞,编译都通不过,调了半天

D. Leaving Auction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n people taking part in auction today. The rules of auction are classical. There were n bids made, though it's not guaranteed they were from different people. It might happen that some people made no bids at all.

Each bid is define by two integers (ai, bi), where ai is the index of the person, who made this bid and bi is its size. Bids are given in chronological order, meaning bi < bi + 1 for all i < n. Moreover, participant never makes two bids in a row (no one updates his own bid), i.e. ai ≠ ai + 1 for all i < n.

Now you are curious with the following question: who (and which bid) will win the auction if some participants were absent? Consider that if someone was absent, all his bids are just removed and no new bids are added.

Note, that if during this imaginary exclusion of some participants it happens that some of the remaining participants makes a bid twice (or more times) in a row, only first of these bids is counted. For better understanding take a look at the samples.

You have several questions in your mind, compute the answer for each of them.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 200 000) — the number of participants and bids.

Each of the following n lines contains two integers ai and bi (1 ≤ ai ≤ n, 1 ≤ bi ≤ 109, bi < bi + 1) — the number of participant who made the i-th bid and the size of this bid.

Next line contains an integer q (1 ≤ q ≤ 200 000) — the number of question you have in mind.

Each of next q lines contains an integer k (1 ≤ k ≤ n), followed by k integers lj (1 ≤ lj ≤ n) — the number of people who are not coming in this question and their indices. It is guarenteed that lj values are different for a single question.

It's guaranteed that the sum of k over all question won't exceed 200 000.

Output

For each question print two integer — the index of the winner and the size of the winning bid. If there is no winner (there are no remaining bids at all), print two zeroes.

Examples
input
6
1 10
2 100
3 1000
1 10000
2 100000
3 1000000
3
1 3
2 2 3
2 1 2
output
2 100000
1 10
3 1000
input
3
1 10
2 100
1 1000
2
2 1 2
2 2 3
output
0 0
1 10
Note

Consider the first sample:

  • In the first question participant number 3 is absent so the sequence of bids looks as follows:
    1. 1 10
    2. 2 100
    3. 1 10 000
    4. 2 100 000
    Participant number 2 wins with the bid 100 000.
  • In the second question participants 2 and 3 are absent, so the sequence of bids looks:
    1. 1 10
    2. 1 10 000
    The winner is, of course, participant number 1 but the winning bid is 10 instead of 10 000 as no one will ever increase his own bid (in this problem).
  • In the third question participants 1 and 2 are absent and the sequence is:
    1. 3 1 000
    2. 3 1 000 000
    The winner is participant 3 with the bid 1 000.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值