【BZOJ】【3850】ZCC Loves Codefires
生活随笔
收集整理的這篇文章主要介紹了
【BZOJ】【3850】ZCC Loves Codefires
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
貪心
就跟NOIP2012國王游戲差不多,考慮交換相鄰兩題的位置,對其他題是毫無影響的,然后看兩題順序先后哪個更優。sort即可。
WA了一次的原因:雖然ans開的是long long,但是在這一句:ans+=time*a[i].k;時,還是需要在time(int類型)前面加上(LL)進行類型強制轉換。
1 /************************************************************** 2 Problem: 3850 3 User: ProgrammingApe 4 Language: C++ 5 Result: Accepted 6 Time:64 ms 7 Memory:2052 kb 8 ****************************************************************/ 9 10 //BZOJ 3850 11 #include<cstdio> 12 #include<cstring> 13 #include<cstdlib> 14 #include<iostream> 15 #include<algorithm> 16 #define rep(i,n) for(int i=0;i<n;++i) 17 #define F(i,j,n) for(int i=j;i<=n;++i) 18 #define D(i,j,n) for(int i=j;i>=n;--i) 19 using namespace std; 20 const int N=100086; 21 typedef long long LL; 22 struct node{ 23 int t,k; 24 bool operator < (const node& b) const { 25 return t*k+(t+b.t)*b.k < b.t*b.k+(t+b.t)*k; 26 } 27 }a[N]; 28 29 int main(){ 30 // freopen("input.txt","r",stdin); 31 int n; 32 scanf("%d",&n); 33 F(i,1,n) scanf("%d",&a[i].t); 34 F(i,1,n) scanf("%d",&a[i].k); 35 sort(a+1,a+n+1); 36 LL time=0; 37 LL ans=0; 38 F(i,1,n){ 39 time+=a[i].t; 40 ans+=(LL)a[i].k*time; 41 } 42 printf("%lld\n",ans); 43 return 0; 44 } View Code?
轉載于:https://www.cnblogs.com/Tunix/p/4207551.html
總結
以上是生活随笔為你收集整理的【BZOJ】【3850】ZCC Loves Codefires的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虚拟机CentOS系统没有UNIX2do
- 下一篇: 掌握jQuery插件开发