Protecting the Flowers 牛客题解

Protecting the Flowers

题目链接:传送门

链接:https://ac.nowcoder.com/acm/problem/25043
来源:牛客网

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 cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.
Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own 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 her barn. Moving cow i to its barn requires 2 × Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.
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.
输入描述:
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
输出描述:
Line 1: A single integer that is the minimum number of destroyed flowers
示例1
输入
复制
6
3 1
2 5
2 3
3 2
4 1
1 6
输出
复制
86

题目大概意思:现在农夫有n头牛,本来关在牛棚里,但是现在跑到花圃里去糟蹋花去了。现在农夫要把牛赶回去,每头牛回去的时间是ti,然后单位时间内糟蹋的花是pi,让我们算一下把牛都赶回去的最少花消耗量。

解题思路:这道题一看就是很明显的贪心,但是我最开始的思路还是有点不够,首先我们肯定可以想到,把单位时间内花消耗量的牛牛先送回去,这样就是一个简单的排序,加上计算。但是这样有一个弊端,并且不够贪心,因为有的牛牛虽然他造的少,但是他走的久,所以我们需要更换他们的排序基准,这样我们才能够保证贪心的思路是正确的。所以我们可以计算他们的消耗和花费时间的占比,那么现在我们就可以用一个结构体来存花费的时间,消耗的花,占比。最后根据占比来排序。

用一个前缀和数组来存前n头牛需要造多少花,我们可以看到n的范围是1e5,如果嵌套for循环肯定是会TLE的。

还有就是注意他的时间是两趟,算上来回。

理论成立,实践开始:

#include <bits/stdc++.h>

#define hh "\n"
#define endl "\n"
#define sss(a) scanf("%d",&a)
#define ll long long
#define  int ll
#define ppp(a) printf("%lld\n",a)
#define sssc(a) scanf("%c",&a)
#define rep(i, a, b) for(int i=a;i<=b;i++)
#define per(i, a, b) for(int i=b;i>=a;i--)
#define mm(a, b) memset(a,b,sizeof(a))
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e6 + 11;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
struct node {
    int x, y;
    double k;
} a[maxn];

bool cmp(node a, node b) {
    return a.k < b.k;
}

int pre[maxn];

signed main() {
    int t;
    cin >> t;
    for (int i = 1; i <= t; i++) {
        cin >> a[i].x >> a[i].y;
        a[i].k = a[i].x * 1.0 / a[i].y * 1.0;

    }
    sort(a + 1, a + 1 + t, cmp);
//    for (int i = 1; i <= t; i++)
//        cout << a[i].x << " " << a[i].y << endl;
    int sum = 0;
    pre[1] = a[1].y;
    for (int i = 2; i <= t; i++) {
        pre[i]=pre[i-1]+a[i].y;
    }
    for(int i = 1; i <= t ; i++){
        sum += (pre[t] - pre[i]) * a[i].x * 2;
    }
    cout << sum << endl;
    return 0;
}

然后这道题就可以快乐AC了,祝大家做题愉快!

  • 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、付费专栏及课程。

余额充值