日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

牛客网 【每日一题】5月28日题目精讲 Protecting the Flowers

發布時間:2023/12/3 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客网 【每日一题】5月28日题目精讲 Protecting the Flowers 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:

文章目錄

    • 題目描述
    • 題意:
    • 題解:
    • 代碼:

時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 32768K,其他語言65536K 64bit IO Format: %lld

題目描述

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頭牛,將第i頭牛運回谷倉需要時間2*ti,在等待運輸過程中每分鐘吃di朵花,問怎么運輸牛才能讓花的損失最小?

題解:

貪心問題
有AB兩頭牛相鄰,
A牛:Ta,Da
B牛:Tb, Db
在運A牛時,B牛在等待,等待了Ta2的時長,每分鐘吃Db多花,一共吃了Ta2Db多花
同理,在運B牛時,A牛在等待,等待了Tb2的時長,每分鐘吃Da多花,一共吃了Tb2Da多花
當運輸A牛比運輸B牛更劃算時,
Ta2Db<Tb2Da
TaDb<TbDa
然后將牛按照這個排序,這個順序就是最佳順序,直接計算花的損失即可

代碼:

tot記錄一分鐘所有等待牛破壞花的數量

#include<bits/stdc++.h> const int maxn=1e5+3; using namespace std; struct node{int t,d; }a[maxn]; bool cmp(node x,node y){return x.t*y.d<x.d*y.t; } int main() {int n;cin>>n;long long sum=0;long long tot=0;for(int i=1;i<=n;i++){cin>>a[i].t>>a[i].d;tot+=a[i].d;}sort(a+1,a+n+1,cmp);for(int i=1;i<=n;i++){ // cout<<"tot="<<tot<<endl;sum+=(tot-a[i].d)*a[i].t*2;tot-=a[i].d;}cout<<sum;return 0; }

總結

以上是生活随笔為你收集整理的牛客网 【每日一题】5月28日题目精讲 Protecting the Flowers的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。