2018 Xiangtan University Programming Competition --- Food (Offline Algorithm)


As a standard foodie, mostshy plans to go to the Lianjian Commercial Street for food again.
Having been in the commercial street for a long time, mostshy already knows all the food and its prices in the commercial street, and he gives each food a delicacy, the higher the delicacy, the more he likes it.
Mostshy wants to know, if you take t yuan to the commercial street and you can only eat one kind of food, what is the highest deliciousness of the food you can taste?

Enter description:

The first line is an integer T (1 ≤ T ≤ 10), representing the number of samples.
In the future, the first line of each sample is two integers n, m (1 ≤ n, m ≤ 30000), indicating the number of types of food and the number of queries.
In the next n lines, two integers in each line represent the price and delicacy di , ci (1 ≤ d i , ci ≤ 10 9 ) of the ith food, respectively.
The next m lines, each line of an integer indicates that mostshy take t (1 ≤ t ≤ 10 9 ) yuan to go to the commercial street for food.

Output description:

Each query outputs one line, an integer, indicating the highest deliciousness of the food that can be tasted in the commercial street with t yuan. If there is no such food, output 0.

enter

1
3 3
1 100
10 1000
1000000000 1001
9
10
1000000000

output

100
1000
1001

This problem requires the use of offline algorithms: offline algorithms are based on the basic assumption that the input data is known before the algorithm is executed. That is, for an offline algorithm, all the inputs to the problem need to be known at the beginning. data, and output results as soon as a problem is solved.

The reason for sorting the number of queries in this question is that when comparing later, you only need to traverse once to reduce the time complexity, otherwise double for loops are required

Structure ordering: a certain ordering, other related numbers will follow that ordering

#include <stdio.h>
#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
	int val;
	int away;
}a[30005];
struct node1
{
	int num;
	int money;
	int years;
}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;
}
intmain()
{
	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].road = a[i-1].road;
			}
		}
		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;
}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326710649&siteId=291194637