bzoj1634:护花

1634: [Usaco2007 Jan]Protecting the Flowers 护花

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 556  Solved: 351
[Submit][Status][Discuss]

Description

Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cows were in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport the cows back to their barn. Each cow i is at a location that is Ti minutes (1 <= Ti <= 2,000,000) away from the barn. Furthermore, while waiting for transport, she destroys Di (1 <= Di <= 100) flowers per minute. No matter how hard he tries,FJ can only transport one cow at a time back to the barn. Moving cow i to the barn requires 2*Ti minutes (Ti to get there and Ti to return). Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小,约翰赶紧采取行动,把牛们送回牛棚. 牛们从1到N编号.第i只牛所在的位置距离牛棚Ti(1≤Ti《2000000)分钟的路程,而在约翰开始送她回牛棚之前,她每分钟会啃食Di(1≤Di≤100)朵鲜花.无论多么努力,约翰一次只能送一只牛回棚.而运送第第i只牛事实上需要2Ti分钟,因为来回都需要时间.    写一个程序来决定约翰运送奶牛的顺序,使最终被吞食的花朵数量最小.

Input

* Line 1: A single integer

N * Lines 2..N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow‘s characteristics

第1行输入N,之后N行每行输入两个整数Ti和Di.

Output

* Line 1: A single integer that is the minimum number of destroyed flowers

一个整数,表示最小数量的花朵被吞食.

Sample Input

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample Output

86

HINT

约翰用6,2,3,4,1,5的顺序来运送他的奶牛.

Source

Silver

早上想了很久贪心贪心,然后贪心不出来就想dpdpdp于是一节语文课还是想不出来,然后看了题解发现其实想差了一点点,因为想的只是贪心一定要某个元素的特征满足,但是这个特征也是可以并不明显或者是推出来的;贪心应用

-----------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
struct node{
 int t,n;
 bool operator<(const node&rhs)const {
     return t*rhs.n<rhs.t*n;}
};
node e[100005];
int main(){
 int m;
 long long sum=0,ans=0;
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
       scanf("%d%d",&e[i].t,&e[i].n);
       sum+=e[i].n;
    }
    sort(e+1,e+m+1);
    for(int i=1;i<=m;i++){
       sum-=e[i].n;
       ans+=2*sum*e[i].t;
    }
    printf("%lld\n",ans);
    return 0;
}

---------------------------------------------------------------------------------------------

时间: 2024-10-14 02:09:00

bzoj1634:护花的相关文章

[bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵花.求一个弄走牛的顺序,使得这些牛破坏最少的花. 注释:$1\le n \le 10^5$. 想法:贪心. 两头牛i和j. 只考虑这两头牛的位置. 如果i在j前面,拉走i的时候j会造成$2t_i*atk_j$朵花.反之同理. 比较两者谁大就放在前面. 在cmp中这样写就行了. 最后,附上丑陋的代码.

[BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花

1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 829  Solved: 537 [Submit][Status][Discuss] Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, as usual. Wh

1634: [Usaco2007 Jan]Protecting the Flowers 护花

1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  Solved: 310[Submit][Status] Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, as usual. When he retur

[BZOJ] 1634: [Usaco2007 Jan]Protecting the Flowers 护花

1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 827  Solved: 536[Submit][Status][Discuss] Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, as usual. When

BZOJ 1634 护花

d/t从大到小排序. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 100500 using namespace std; long long n,ans=0; struct pnt { long long d,t,id; }p[maxn]; bool cmp(pnt x,pnt y) { return (double)x.d/x.

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734

高考语文必备古诗文64篇

54.阿房宫赋 (杜牧) 六王毕,四海一.蜀山兀,阿房出.覆压三百余里,隔离天日.骊山北构而西折,直走咸阳.二川溶溶,流入宫墙.五步一楼,十步一阁:廊腰缦回,檐牙高啄:各抱地势,钩心斗角.盘盘焉,囷囷焉,蜂房水涡,矗不知其几千万落.长桥卧波,未云何龙?复道行空,不霁何虹?高低冥迷,不知西东.歌台暖响,春光融融:舞殿冷袖,风雨凄凄.一日之内,一宫之间,而气候不齐. 妃嫔媵嫱,王子皇孙,辞楼下殿,辇来于秦.朝歌夜弦,为秦宫人.明星荧荧,开妆镜也:绿云扰扰,梳晓鬟也:渭流涨腻,弃脂水也:烟斜雾横,焚椒

心若向阳,何惧忧伤

有一种光明我们唤其名为阳光,有一种心态我们唤其名为向阳 . --题记 风从水上走过,留下粼粼波纹:骆驼从沙漠上走过,留下深深 的脚印:哨鸽从天空飞过,留下串串欢韵:岁月从树林穿过,留下 圈圈年轮.时光飞逝,你是否有一丝忧伤? "相思相见知何日?此时此夜难为情"李白难解秋风秋月里的 忧伤;"君若扬路尘,妾若浊水泥,浮沈各异势,会合何时谐?" 曹植难解崎岖境遇的忧伤:"落红不是无情物,化作春泥更护花" 龚自珍难解离别官场的忧伤;"劝君更尽一

月2 周5 (前半写于周2)

<天镜的阿尔德拉民> 8/12 <天镜的阿尔德拉民7> 10/12 <零之魔法书> 7/12 <剑刻的银乙女> 7/12 <一个人也能活下去> 6/12 <异世界和我,你喜欢哪一个>(七夕节狗粮啊) 5/12 <自称贤者弟子的贤者> 4/12 <异世界料理道> 7/12 <精灵幻想记> 3/12 <说谎的天使死了> 9/12 <欢迎来到实力至上主义教室> 8/12 <庶