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

时间: 2024-10-10 20:26:58

sdut2168--Mathmen(贪心)的相关文章

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

CoderForce 140C-New Year Snowmen(贪心)

题目大意:有n个已知半径的雪球.堆一个雪人需要三个尺寸不同的雪球,问用这些雪球最多能堆多少个雪人? 题目分析:先统计一下每种尺寸的球的个数,从三种最多的种类中各取出一个堆成雪人,这样贪心能保证的到的数目最多. 代码如下: # include<iostream> # include<map> # include<vector> # include<cstdio> # include<queue> # include<algorithm>

计蒜客 跳跃游戏(贪心)

给定一个非负整数数组,假定你的初始位置为数组第一个下标.数组中的每个元素代表你在那个位置能够跳跃的最大长度. 请确认你是否能够跳跃到数组的最后一个下标. 例如: A = [2,3,1,1,4], return ture A = [3,2,1,0,4], return false. 格式: 第一行输入一个正整数n,接下来的一行,输入数组A[n].如果能跳到最后一个下标,输出"true",否则输出"false" 样例1 ????输入:???? ????????5 ???

BZOJ 2525 [Poi2011]Dynamite 二分+树形贪心

题意: n个点,一棵树,有些点是关键点,可以将m个点染色. 求所有关键点到最近的被染色点的距离的最大值最小. 解析: 反正从这道题我是学了一种做题思路? 大爷讲课的时候说的:一般选择某些点的代价相同的话都是贪心,代价不同的话一般都是DP. 想想也挺对的,不过就是没有感悟到过? 反正这题考试的时候我是直接D了贪心的- -! 忘了为啥D了. 显然最大值最小我们需要二分一下这个值. 然后接下来我们从下往上扫整棵树. 节点的状态有几个? 第一种是 子树内没有不被覆盖的关键点,并且子树中有一个节点的贡献可

CF 500 C. New Year Book Reading 贪心 简单题

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi. As Jaehyun's house is not large enough to have a bookshelf, he k

贪心 --- HNU 13320 Please, go first

Please, go first Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13320 Mean: n个人一起去滑雪,要坐电梯到山顶,电梯每5分钟可以送一个人上去.这n个人中有的是组好团一起滑雪的,所以要等到齐了才能开始滑. 但是他们到达电梯下的时间都不是统一的,对于单独一个人来说,他在山下等和在山上等的时间都是一样的. 但是对于n个人的集体来说,如果让他后面的人先上去,这样可能会更节省时间.

二分+贪心

上海邀请赛热身时候,C题是一个二分+贪心的题目.起初并不会,问了旁边的复旦大神.这几天无意发现VJ上一个专题.擦原来是一个经典类型. 二分+贪心 这类题目注意数据范围,1e8,1e9一般都是这样. 注意事项 二分法有很多写法,推荐用lf+1 < rf的写法.这个也符合计算机中数据存取的原则.对于浮点数,直接就循环100次,精度绝对够. 一般有两种类型,一种是询问最优,即数列中无重复.一种是多个即lower_bound ,upper_bound这类函数问题. 贪心使用,就是这个问题枚举答案可被验证