Mathmen love mathematics, and they live on the number line. All the mathmen spend all their time on solving mathematical problems and proving theorems. Like humen beings, mathmen don't live in the same country there are n mathmen countries on difierent positions of the number line (country i lives at position xi (one point on the number line), and xi < xi + 1 for all 1 <= i <= n - 1). Mathmen in difierent countries are good at difierent areas of mathematics. For example, mathmen living in country 1 are good at geometry, while mathmen living in country 2 are experts on algebra.
Lately, all the mathmen nations have collaborated on a huge problem, which involves all areas of mathematics, and each mathmen country is responsible for solving the part they are good at. After hours of work (they are really good at mathematics, and no problem would cost them more than one day to solve), they all finish their jobs. Now, they need to collect the result in every mathmen country, and combine them together to create the solution to the huge problem. So they decide to let one mathman collect all the results and send it to one country, where all the work will be merged.
As mathmen are devoted to solving mathematical problems, their legs has become very weak after millions of years of evolution. Therefore, they have to ride mathships to travel on the number line. Their are M types of mathships, each of which has a limit of traveling distance and a cost of IQ (yes, you need to be very brave to take mathships, you would never be able to get back the lost IQ).
There are two seasons in the mathmen world Positive and Negative. Now it is Positive, so all the mathships travels on the number line from left to right. Therefore, one man from country 1 must be selected as the volunteer to collect the results in every country and send to to country n.
There is at least one mathship of each type in every country. So, after picking the results from country 1, the volunteer needs to select one mathship with a limit of traveling distance at least the distance between country 1 and country 2 (the distance is x2 - x1), and ride it from country 1 to country 2.
Meahwhile, this trip will cost him the corresponding amount of IQ. Then, he picks the result from country 2, choose another suitable mathship (the old mathship will be maintained in country 2, and can not continue to be used), and ride it to country 3, and lose some IQ. He will repeat this process, until he reaches country n.
Mathmen care about their IQ a lot, so the volunteer wants to minimize the total cost of his IQ. Could you please write program to help him select the right mathships to take?
Each test case begins with a line containing two integers, n (2 <= n <= 10000) and m (1 <= m <= 100000), which is the number of mathmen countries and the number of mathship types. The next line contains n integers, ranging from -10^9to10^9(inclusive), indicating the positions of the mathmen countries. Then, m lines follows, each containing two non-negative integers no more than 2*109, which are the limit of traveling distance and the cost of IQ.
2 3 3 0 3 10 1 0 6 1 10 10 2 1 0 1000 100 0
11 Impossible
题目大意是给出n个城市每个城市的坐标递增,每个城市中有m个交通工具,每个交通工具有可以行进的距离和耗油,不满最大距离时,按最大距离算,问能不能从0号城市走到n-1号城市。
对每个城市之间的距离进行由大到小排序,然后对没见交通工具可以走的距离进行排序,然后用优先队列找出最小的值。
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> using namespace std ; #define LL long long priority_queue <LL,vector<LL>,greater<LL> > que ; struct node{ LL x , y ; }p[110000]; LL a[11000] , dis[11000] ; int cmp(node a,node b) { return a.x < b.x ; } int main() { int t , n , m ; LL ans ; int i , j ; scanf("%d", &t) ; while( t-- ) { ans = 0 ; scanf("%d %d", &n, &m) ; for(i = 0 ; i < n ; i++) { scanf("%lld", &a[i]) ; if( i ) dis[i] = a[i] - a[i-1] ; } for(i = 0 ; i < m ; i++) scanf("%lld %lld", &p[i].x, &p[i].y) ; sort(dis+1,dis+n) ; sort(p,p+m,cmp) ; while( !que.empty() ) que.pop() ; j = m-1 ; for(i = n-1 ; i >= 1 ; i--) { while( j >= 0 && p[j].x >= dis[i] ) { que.push(p[j].y) ; j-- ; } if( que.empty() ) break ; ans += que.top() ; } if( i >= 1 ) printf("Impossible\n") ; else printf("%lld\n", ans) ; } return 0 ; }