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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[NOI2007]货币兑换Cash(DP+动态凸包)

發(fā)布時(shí)間:2025/7/14 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [NOI2007]货币兑换Cash(DP+动态凸包) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一次打動(dòng)態(tài)凸包維護(hù)dp,感覺學(xué)到了超級(jí)多的東西。

首先,set是如此的好用!!!可以通過(guò)控制一個(gè)flag來(lái)實(shí)現(xiàn)兩種查詢,維護(hù)凸包和查找斜率k

不過(guò)就是重載運(yùn)算符和一些細(xì)節(jié)方面有些惡心,90行解決

后面還有一個(gè)cdq分治,找時(shí)間學(xué)下,看下能不能處理一大類惡心的問題

github還是不會(huì)用,找時(shí)間搞下吧

CODE:

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<set> 6 #include<cmath> 7 using namespace std; 8 const double esp=1e-7; 9 const int maxn=101000; 10 int dcmp(double a) { 11 if (fabs(a)<esp) return 0; 12 return a<0?-1:1; 13 } 14 struct node{ 15 double x,y,k;bool f; 16 node(double _x,double _y):x(_x),y(_y),f(0){} 17 node():f(0){} 18 inline bool error(){return x>1e50;} 19 }error(1e51,0); 20 inline bool operator == (const node x,const node y) {return dcmp(x.x-y.x)==0;} 21 inline bool operator < (const node x,const node y){ 22 if (x.f||y.f) return dcmp(x.k-y.k)>0; 23 else return dcmp(x.x-y.x)<0; 24 } 25 inline double operator *(const node x,const node y) {return x.x*y.y-x.y*y.x;} 26 inline node operator -(const node x,const node y){ 27 node tmp; 28 tmp.x=x.x-y.x;tmp.y=x.y-y.y; 29 return tmp; 30 } 31 inline double calc_k(node x,node y) {return (x.y-y.y)/(x.x-y.x);} 32 set<node> gap; 33 typedef set<node>::iterator iter; 34 inline node lower(node x) { 35 iter it=gap.lower_bound(x); 36 return it==gap.end()?error:*it; 37 } 38 inline node next(node x) { 39 iter it=gap.upper_bound(x); 40 return it==gap.end()?error:*it; 41 } 42 inline node pre(node x) { 43 iter it=gap.lower_bound(x); 44 return it==gap.begin()?error:*(--it); 45 } 46 inline void ins(node a) { 47 if (gap.empty()) {a.k=0;gap.insert(a);return;} 48 node d1=pre(a),d2=lower(a); 49 if (d1.error()&&d2.y>a.y) return ; 50 if ((!d1.error())&&(!d2.error())&&(dcmp((d2-d1)*(a-d1))<=0)) return ; 51 if (d2==a) return; 52 node p1,p2=next(a); 53 for (;;){ 54 p1=p2;p2=next(p2); 55 if (p1.error()||p2.error()) break; 56 if (dcmp((p1-a)*(p2-a))<=0) break; 57 gap.erase(p1); 58 } 59 p2=pre(a); 60 for (;;){ 61 p1=p2;p2=pre(p2); 62 if (p1.error()||p2.error()) break; 63 if (dcmp((p1-a)*(p2-a))>=0) break; 64 gap.erase(p1); 65 } 66 d1=pre(a),d2=next(a); 67 if (d1.error()) a.k=0;else a.k=calc_k(d1,a);gap.insert(a); 68 if (!d2.error()) gap.erase(d2),d2.k=calc_k(a,d2),gap.insert(d2); 69 } 70 inline double get_k(double a,double b) { 71 node tmp;tmp.f=1;tmp.k=-a/b; 72 tmp=*(--gap.lower_bound(tmp)); 73 return a*tmp.x+b*tmp.y; 74 } 75 double a[maxn],b[maxn],r[maxn],na[maxn],nb[maxn],f[maxn]; 76 int main(){ 77 int n,s; 78 scanf("%d%d",&n,&s); 79 for (int i=1;i<=n;i++) scanf("%lf%lf%lf",a+i,b+i,r+i); 80 f[1]=s; 81 nb[1]=f[1]/(a[1]*r[1]+b[1]); 82 na[1]=nb[1]*r[1]; 83 ins(node(na[1],nb[1])); 84 for (int i=2;i<=n;i++) { 85 f[i]=max(f[i-1],get_k(a[i],b[i])); 86 nb[i]=f[i]/(a[i]*r[i]+b[i]); 87 na[i]=nb[i]*r[i]; 88 ins(node(na[i],nb[i])); 89 } 90 printf("%.3f\n",f[n]); 91 } 92

?

  

轉(zhuǎn)載于:https://www.cnblogs.com/New-Godess/p/4354675.html

總結(jié)

以上是生活随笔為你收集整理的[NOI2007]货币兑换Cash(DP+动态凸包)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。