作为一个标准的吃货,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 3 3 1 100 10 1000 1000000000 1001 9 10 1000000000
输出
100 1000 1001
这题需要用到离线算法:离线算法( off line algorithms),是指基于在执行算法前输入数据已知的基本假设,也就是说,对于一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果。
该题中给查询次数排序的原因是,后面比较的时候有 ,只需要遍历一遍即可,降低时间复杂度,否则要双重for循环
结构体排序:某一个排序,其他与之相关的数会跟随那个排序的
#include <stdio.h>
#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
int val;
int weg;
}a[30005];
struct node1
{
int num;
int money;
int ans;
}b[30005];
bool cmp( node a , node b)
{
return a.val<b.val;
}
bool cmp1(node1 x , node1 y)
{
return x.money < y.money;
}
bool cmp2(node1 n , node1 m)
{
return n.num < m.num;
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d %d" , &n , &m);
for(int i = 0 ; i < n ; i++)
{
scanf("%d %d" , &a[i].val,&a[i].weg);
}
sort(a,a+n,cmp);
for(int i = 1 ; i < n ; i++)
{
if(a[i].weg < a[i-1].weg)
{
a[i].weg = a[i-1].weg;
}
}
for(int j = 0 ; j < m ; j++)
{
b[j].num = j;
scanf("%d",&b[j].money);
}
sort(b,b+m,cmp1);
int maxn=0 , r = 0;
for(int i = 0 ; i < m ; i++)
{
while(b[i].money>=a[r].val)
{
maxn = max(maxn,a[r].weg);
if(r >= n)
{
break;
}
r++;
}
b[i].ans = maxn;
}
sort(b, b+m,cmp2);
for(int i = 0 ; i < m ; i++)
{
printf("%d\n" , b[i].ans);
}
}
return 0;
}