POJ_3262 Protecting the Flowers 【贪心】
生活随笔
收集整理的這篇文章主要介紹了
POJ_3262 Protecting the Flowers 【贪心】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、題面
POJ3262
二、分析
這題要往貪心上面想應該還是很容易的,但問題是要證明為什么比值關系就能滿足。
可以選擇幾個去分析,入1-6? 與 2-15? 和 1-6 與2-5 和 1-6 與 2- 12。
三、AC代碼
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <fstream> 5 using namespace std; 6 7 const int MAXN = 1e5+6; 8 9 struct Cow 10 { 11 int time, flower; 12 }Data[MAXN]; 13 14 bool Cmp(const Cow a, const Cow b) 15 { 16 return (double)a.flower/a.time > (double)b.flower/b.time; 17 } 18 19 int main() 20 { 21 //freopen("input.txt", "r", stdin); 22 int N; 23 scanf("%d", &N); 24 for(int i = 0; i < N; i++) 25 { 26 scanf("%d %d", &Data[i].time, &Data[i].flower); 27 } 28 sort(Data, Data+N, Cmp); 29 long long T = 0, Ans = 0; 30 for(int i = 0; i < N; i++) 31 { 32 Ans += Data[i].flower*T; 33 T += 2*Data[i].time; 34 } 35 printf("%I64d\n", Ans); 36 return 0; 37 } View Code?
轉載于:https://www.cnblogs.com/dybala21/p/10150761.html
總結
以上是生活随笔為你收集整理的POJ_3262 Protecting the Flowers 【贪心】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++11并发编程:多线程std::th
- 下一篇: jquery如何阻止子元素继承父元素的事