湖南大学第十五届程序设计竞赛(重现赛)-D-Kth height

链接:https://ac.nowcoder.com/acm/contest/908/D
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

    There are two teams on the playground. The number of people in each team is exactly n. At the same time, for each team, their height is increasing in order. It means that, for each team, if i < j, then hi < hj. Because the weather is too hot, some people choose to find someone to replace them in the queue. In order not to be discovered, it is necessary to ensure that the replaced team is still increasing in height.

    After each replacement, Tom wants to know who is the k-th tallest of the two teams. He is too lazy to find the answer, so he ask you to help him!

输入描述:

There are multiple test cases so please read to the end of file. For each test case, the first line is an integer n, the size of two teams. Two lines follow, each line consists of n integers. The second line represents the first team's height(from 1 to n). The third line represents the second team's height. Then follow an integer m, which is the total number of replacement. Follow m lines, the i-th line contains four integers pi, qi, ti, ki, where
p represents which team it belong to;
q represents the q-th people of p-th team will change;
t represents the height of  the q-th people of p-th team will be changed to t;
k stands for the k-th tallest height Tom wants to know.
You can assume that no one's height will be larger than 1000000000.

1<=n,m<=100000
1 <=pi<=2
1<=qi<=n
1<=ti<=1000000000
1<=ki<=n

输出描述:

After each replacement, output the k-th tallest height of the two teams.

示例1

输入

复制

10
1 100 200 300 400 500 600 700 800 900
2 101 105 108 109 201 301 401 402 505
5
1 2 55 3
1 1 44 4
1 3 66 5
1 4 77 6
1 5 88 7

输出

复制

55
101
101
101
101

地址:https://ac.nowcoder.com/acm/contest/908/D

思路:二分查找,题目要求在更改元素的情况下查找两数组的第k小的值,由于两数组都是始终有序的,因此可以二分查找答案,即查找答案h是否为两数组的第k小。

注意的是如何使二分查找的最终答案是两数组里面的元素。对于二分查找,当找第k大时,二分出来的答案ans是最大的第k小,即ans+1就是第(k+1)小。因此可以二分查找第(k-1)小的值,然后+1即可.

Code:

#include<iostream>
#include<algorithm>
using namespace std;

const int MAX_N=1e5+5;
int n,m;
int a[MAX_N],b[MAX_N];

int query(int k){
    int l=0,r=1e9+7,h,t1,t2;
	while(l<=r){
		h=(l+r)/2;
		t1=lower_bound(a,a+n,h)-a;
		t2=lower_bound(b,b+n,h)-b;
		if(t1<n&&a[t1]==h)	++t1;
		if(t2<n&&b[t2]==h)	++t2;
		if(t1+t2<=k)	l=h+1;
		else	r=h-1;
	}
	return r+1;
}
int main()
{
	ios::sync_with_stdio(false);
	while(cin>>n){
        for(int i=0;i<n;++i)
            cin>>a[i];
        for(int i=0;i<n;++i)
            cin>>b[i];
        cin>>m;
        int p,q,t,k;
        while(m--){
            cin>>p>>q>>t>>k;
            if(p==1)	a[q-1]=t;
            else	b[q-1]=t;
            cout<<query(k-1)<<endl;
        }
    }
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值