链接:https://www.nowcoder.com/acm/contest/105/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
作为一个标准的吃货,mostshy又打算去联建商业街觅食了。
混迹于商业街已久,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风格的输入输出。
题解:
这道题排完序然后数组优化,直接二分查找就行了(不要从前往后找,会超时)
附ac代码:
#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a<b;
}
int main()
{
int t;
int n,m;
int money;
scanf("%d",&t);
map<int,int> ma;
int d[30000+10];
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
d[i]=a;
if(b>ma[a]) ma[a]=b;
}
sort(d,d+n,cmp);
for(int i=0;i<m;i++)
{
int flag=1;
scanf("%d",&money);
int index=lower_bound(d,d+n,money)-d;
if(d[index]==money) printf("%d\n",ma[d[index]]);
else if(index==n){
printf("%d\n",ma[d[n-1]]);
}
else if(index==0){
if(money>=d[0]){
printf("%d\n",ma[d[0]]);
}
else printf("-1\n");
}
else printf("%d\n",ma[d[index-1]]);
}
}
return 0;
}