链接:
https://www.nowcoder.com/acm/contest/105/E
来源:牛客网
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
作为一个标准的吃货,mostshy又打算去联建商业街觅食了。
混迹于商业街已久,mostshy已经知道了商业街的所有美食与其价格,而且他给每种美食都赋予了一个美味度,美味度越高表示他越喜爱这种美食。
mostshy想知道,假如带t元去商业街,只能吃一种食物,能够品味到的美食的美味度最高是多少?
混迹于商业街已久,mostshy已经知道了商业街的所有美食与其价格,而且他给每种美食都赋予了一个美味度,美味度越高表示他越喜爱这种美食。
mostshy想知道,假如带t元去商业街,只能吃一种食物,能够品味到的美食的美味度最高是多少?
输入描述:
第一行是一个整数T(1 ≤ T ≤ 10),表示样例的个数。 以后每个样例第一行是两个整数n,m(1 ≤ n,m ≤ 30000),表示美食的种类数与查询的次数。 接下来n行,每行两个整数分别表示第i种美食的价格与美味度di,ci (1 ≤ di,ci ≤ 109)。 接下来m行,每行一个整数表示mostshy带t(1 ≤ t ≤ 109)元去商业街觅食。
输出描述:
每个查询输出一行,一个整数,表示带t元去商业街能够品味到美食的最高美味度是多少,如果不存在这样的美食,输出0。
示例1
输入
1 3 3 1 100 10 1000 1000000000 1001 9 10 1000000000
输出
100 1000 1001
说明
大量的输入输出,请使用C风格的输入输出。
题解:首先,先对所有美食的价钱进行排序,从小到大,排完序以后在修改美味度的值,即如果美食a的di < 美食b的di 且a.ci > b.ci 则可以将 b.ci 改为a.ci,然后在使用二分搜索即可(二分的时候处理等号,l,r,mid的时候wa了好多次,太菜了)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 30010;
struct food{
int di;
int ci;
}f[maxn];
int ma[maxn];
int t, n, m;
bool cmp(food a, food b){
if(a.di == b.di){
return a.ci < b.ci;
}
return a.di < b.di;
}
int main(){
scanf("%d", &t);
while(t--){
memset(f, 0, sizeof(f));
memset(ma, 0, sizeof(ma));
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++){
scanf("%d%d", &f[i].di, &f[i].ci);
}
sort(f+1, f+1+n, cmp);
for(int i=1; i<=n; i++){
f[i].ci = max(f[i-1].ci, f[i].ci);
}
while(m--){
int temp;
scanf("%d", &temp);
int l = 1;
int r = n;
int mid;
int ans = 0;
while(l <= r){
mid = (l+r)/2;
if(f[mid].di <= temp){
ans = mid;
l = mid+1;
}
else r = mid-1;
}
// cout<<l<<" "<<mid<<endl;
printf("%d\n", f[ans].ci);
}
}
}