Protecting the Flowers(3262)

http://poj.org/problem?id=3262

题解:
考虑当前选哪个放在开头。
设放在开头的为(d1,t1) 放在第二个的为(d2,t2)。
那么放在开头的牛满足:无论第二个是什么,交换第一和第二总是不会变优。
设当前剩余牛的d值和为ds。
那么有:
(ds-d1)*t1+(ds-d1-d2)*t2<(ds-d2)*t2+(ds-d2-d1)*t1
化简得:
 t2/d2>t1/d1。
也就是说,当t[i]/d[i]>t[j]/t[j]时,j放在i前面更优。
若开头的是t[i]/d[i]中的最小值,无论后面是什么,它永远是最优的。
那么我们可以确定开头了。
而注意到开头确定后,剩下的正好是n-1规模的子问题,可以用一样的方式解决。
所以我们可以按t[i]/d[i]排序,从小取到大就是最终序列了。
复杂度:O(nlogn)。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct s{
    int d, t;
}pp[100100];
bool cmp(s a, s b){
    return a.d/(a.t*1.0) < b.d/(b.t*1.0); 
}
int n;
int main(){
    int i;
    while(scanf("%d", &n) == 1){
        long long sum = 0;
        for(i = 1; i <= n; i++){
            scanf("%d%d", &pp[i].d, &pp[i].t);
            sum += pp[i].t;
        }
        sort(pp+1, pp+1+n, cmp);
        long long ans = 0;
        for(i = 1; i <= n; i++){
            sum -= pp[i].t;
            ans += pp[i].d * 2 * sum;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
阅读下面材料,在空格处填入适当的内容(1个单词)或使用括号中单词的正确形式。 If including inter-state sections, those not protecting the northern border of China, the oldest _____56____(exist) section of the Great Wall of China was the Qi State "Great Wall". It stretches for over 500 kilometers (300 miles) from the Yellow River at Jinan eastwards to the East China Sea, almost ___57____(divide) Shandong Province in half. The "Great Wall" of the Qi State was ___58____(initial) built around 650 BC, and expended during the Warring States Period (475–221 BC). Before the Qi State Wall was built, natural barriers, i.e. rivers and mountain ranges, formed ____59_____ only defensible boundaries between territories as barriers ___60____ enemies. The State of Qi built its Great-Wall-esque military barrier along its southern border to prevent attacks from the State of Lu and the State of Chu. However, rapid development and ____61____(construct) have brought many new problems and challenges in protecting the wall. ____62____ is necessary to provide a solid legal guarantee for its conservation. To tackle the challenges, Shandong Province has passed a regulation protecting the structure____63____will take effect on Jan. 1. This year, Shandong has added 860 patrol posts in seven cities along the Qi wall, mainly recruiting farmers living nearby. Guo Jialian has been patrolling the section of the wall in Guangli village for three months. "I need to check ____64______ there is any damage to the wall that is caused by people digging earth from it. Awareness of protecting the Qi wall _____65_____ (enhance) in recent years. We all know the wall is a cultural relic," says Guo, adding that he frequently sees tourists coming to visit the ancient structure.
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值