Cross the Wall UVALive - 5097 (贪心+斜率dp)
2017-10-25 21:17
429 查看
前几天做的一道题。这个题感觉还是蛮好的,之后的要注意这方面的贪心
题意:有n个人要通过墙,每个人有高度h和宽度w,我们最多可以建k个门。第i个人要通过墙的条件是门的高H>hi,门的宽W>wi。建一个高H,宽W的门,代价是W*H。问这n个人过墙的最少的代价
分析:先贪心去重。如果两个人(w1,h1),(w2,h2),如果w1<=w2&&h2<=h2,那么第一个人就可以不用考虑了,因为如果第二个人可以通过,第一个人肯定可以通过
然后按照h从大到小排序,如果h相同,w从大到小。这个时候留下的人
if( i< j)
hi>=hj
wi < wj
所以h是从大到小,w是严格从小到大排序的
状态转移是
dp[k][i]=min(dp[k-1][j]+h[i]*w[j+1]);
题意:有n个人要通过墙,每个人有高度h和宽度w,我们最多可以建k个门。第i个人要通过墙的条件是门的高H>hi,门的宽W>wi。建一个高H,宽W的门,代价是W*H。问这n个人过墙的最少的代价
分析:先贪心去重。如果两个人(w1,h1),(w2,h2),如果w1<=w2&&h2<=h2,那么第一个人就可以不用考虑了,因为如果第二个人可以通过,第一个人肯定可以通过
然后按照h从大到小排序,如果h相同,w从大到小。这个时候留下的人
if( i< j)
hi>=hj
wi < wj
所以h是从大到小,w是严格从小到大排序的
状态转移是
dp[k][i]=min(dp[k-1][j]+h[i]*w[j+1]);
#define ll long long #define mem(a,b) memset(a,b,sizeof(a)) #define inf 0x3f3f3f3f using namespace std; const int maxn = 5e4+10; struct node { ll h,w; }aa[maxn],a[maxn]; int q[maxn]; ll dp[105][maxn]; int n,m; int input() { char ch=' '; while(ch<'0'||ch>'9')ch=getchar(); int x=0; while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar(); return x; } bool cmp(node a1,node a2) { if(a1.h!=a2.h) return a1.h>a2.h; else return a1.w>a2.w; } ll y(int k,int j,int t) { return dp[k-1][j]-dp[k-1][t]; } ll x(int j,int t) { return a[t+1].h-a[j+1].h; } ll getdp(int k,int i,int j) { return dp[k-1][j]+a[i].w*a[j+1].h; } int main() { while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++){ aa[i].h=input();aa[i].w=input(); } mem(a,0); sort(aa+1,aa+n+1,cmp);int cnt=1; a[1].w=aa[1].w,a[1].h=aa[1].h; for(int i=2;i<=n;i++){ if(aa[i].w<=a[cnt].w) continue; a[++cnt].h=aa[i].h,a[cnt].w=aa[i].w; } n=cnt; for(int i=1;i<=n;i++) dp[1][i]=getdp(1,i,0); m=min(m,n); for(int k=2;k<=m;k++){ int head=0,tail=0;q[tail++]=0; for(int i=1;i<=n;i++) { while(head+1<tail&&y(k,q[head+1],q[head])<=a[i].w*x(q[head+1],q[head])) head++; dp[k][i]=getdp(k,i,q[head]); while(head+1<tail&&y(k,i,q[tail-1])*x(q[tail-1],q[tail-2])<=y(k,q[tail-1],q[tail-2])*x(i,q[tail-1])) tail--; q[tail++]=i; } } printf("%lld\n",dp[m] ); } return 0; }
相关文章推荐
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- UVALive - 5097 Cross the Wall(斜率优化)
- UVALive 5097 Cross the Wall
- UVALive 5097(斜率dp)
- HDU 3669 Cross the Wall (斜率优化DP)
- UVALive - 5097(斜率DP)
- hdu 3669 Cross the Wall(斜率优化DP)
- 【斜率优化dp】HDU3669 Cross the Wall
- 动态规划DP的斜率优化 个人浅解 附HDU 3669 Cross the Wall
- UVALive 5097 斜率DP
- UVALive 3530 Martian Mining(贪心,dp)
- UVALive - 3942 Remember the Word[Trie DP]
- UVaLive 4731 UVa 1456 - Cellular Network(概率DP 贪心)