sdut2168--Mathmen(贪心)

2024-08-25 00:58
文章标签 贪心 mathmen sdut2168

本文主要是介绍sdut2168--Mathmen(贪心),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Mathmen

Time Limit: 1000MS Memory limit: 65536K

题目描述

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?

输入

The input contains multiple test cases. The first line of the input is an integer  C, indicating the number of test cases in the input.

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.

输出

For each test case, output one line containing the minimum cost of IQ. If the  volunteer can never reach country n, output "Impossible" (without quotation  marks).

示例输入

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 ;
}

这篇关于sdut2168--Mathmen(贪心)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104108

相关文章

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

poj 3190 优先队列+贪心

题意: 有n头牛,分别给他们挤奶的时间。 然后每头牛挤奶的时候都要在一个stall里面,并且每个stall每次只能占用一头牛。 问最少需要多少个stall,并输出每头牛所在的stall。 e.g 样例: INPUT: 51 102 43 65 84 7 OUTPUT: 412324 HINT: Explanation of the s

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

POJ2010 贪心优先队列

c头牛,需要选n头(奇数);学校总共有f的资金, 每头牛分数score和学费cost,问合法招生方案中,中间分数(即排名第(n+1)/2)最高的是多少。 n头牛按照先score后cost从小到大排序; 枚举中间score的牛,  预处理左边与右边的最小花费和。 预处理直接优先队列贪心 public class Main {public static voi

ural 1820. Ural Steaks 贪心

1820. Ural Steaks Time limit: 0.5 second Memory limit: 64 MB After the personal contest, happy but hungry programmers dropped into the restaurant “Ural Steaks” and ordered  n specialty steaks

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

每日一题|牛客竞赛|四舍五入|字符串+贪心+模拟

每日一题|四舍五入 四舍五入 心有猛虎,细嗅蔷薇。你好朋友,这里是锅巴的C\C++学习笔记,常言道,不积跬步无以至千里,希望有朝一日我们积累的滴水可以击穿顽石。 四舍五入 题目: 牛牛发明了一种新的四舍五入应用于整数,对个位四舍五入,规则如下 12345->12350 12399->12400 输入描述: 输入一个整数n(0<=n<=109 ) 输出描述: 输出一个整数

BUYING FEED(贪心+树状动态规划)

BUYING FEED 时间限制: 3000 ms  |  内存限制: 65535 KB 难度:4 描述 Farmer John needs to travel to town to pick up K (1 <= K <= 100)pounds of feed. Driving D miles with K pounds of feed in his truck costs D

vua 10700-Camel trading 贪心以及栈

大意:给一个表达式,可以让你任意套括号,问套完括号最大最小值是多少 贪心策略:最大的话,先+后*                  最小的话,先*后+ 用了一个栈堆模拟运算的次序 #include<stdio.h>#include<iostream>#include<stack>using namespace std;int main(){int N;scanf("%d",&

Commando War-uva 贪心

大意:给你N个任务,你交代他需要J时间,完成他需要B时间,问怎么搭配可以使全部问题完成时话的时间最少 思路:贪心算法,先做完成时间长的,完成时间相同的话先做交代时间长的,用了一下结构体二级快排 #include<stdio.h>#include<string.h>#include<stdlib.h>#define MAX_SIZE 1000 + 10struct Time{int