fzu 2077 The tallest tree

Problem 2077 The tallest tree

Accept: 89    Submit: 335
Time Limit: 1000 mSec    Memory Limit : 65536 KB

Problem Description

lzs种了n棵树,每棵树每天长高一定的高度。某一天,lzs想知道树长得怎么样了,你能求出那一天最高的树有多高吗?

Input

有 多组测试数据,每组数据第一行输入两个整数n,m(1<=n,m<=100000),接下来n行,每行两个整数a,b(0<=a,b& lt;=100000),表示第i棵树在第0天的高度以及每天生长的高度。接下来m行,每行一个整数x(0<=x<=100000),表示询 问第x天最高的树有多高。

Output

对于每个询问输出一行,为当天最高的树的高度。

Sample Input

1 3
10 4
1
2
3

Sample Output

14
18
22
这个题1A,感觉有点欣喜,昨天没有思路,昨天晚上才想到思路。用到贪心,把每个颗树先按生长速度排序,然后再按高度排序,这样就满足结构体下标大的生长速率一定小于
前面的生长速率,如果在第i天后面的树的总高度还没有比前面的树高的话,那么这棵树就没有机会在以后的时间赶上前面的树了,因为其生长速率要小于等与前面的速率就应该把这
棵树排除。感觉有点像单调队列的思想
View Code
 1 #include <iostream>
2
3 using namespace std;
4 typedef long long ll;
5 const int maxn=100010;
6
7 struct Node
8 {
9 ll h,rat;
10 }node[maxn];
11 int cmp(Node a,Node b)
12 {
13 if(a.rat!=b.rat) return a.rat > b.rat;
14 return a.h > b.h;
15 }
16 ll t1[maxn],t2[maxn],ans[maxn];
17
18 int main()
19 {
20 int n,m;
21 while(scanf("%d %d",&n,&m)==2)
22 {
23 for(int i=0;i<n;i++)
24 scanf("%lld %lld",&node[i].h,&node[i].rat);
25 sort(node,node+n,cmp);
26 for(int i=0;i<m;i++)
27 {
28 scanf("%lld",&t1[i]);
29 t2[i]=t1[i];
30 }
31 sort(t2,t2+m);
32 int top=n;
33 for(int i=0;i<m;i++)
34 {
35 int p=0;
36 for(int j=1;j<top;j++)
37 {
38 if(node[j].h+node[j].rat*t2[i]>node[p].h+node[p].rat*t2[i])
39 node[++p]=node[j];
40 }
41 ans[t2[i]]=node[p].h+node[p].rat*t2[i];
42 top=p+1;
43 }
44 for(int i=0;i<m;i++)
45 printf("%lld\n",ans[t1[i]]);
46 }
47 return 0;
48 }

posted on 2012-03-30 10:32  Goal  阅读(243)  评论(0编辑  收藏  举报

导航