zoukankan      html  css  js  c++  java
  • 【集训笔记】动态规划背包问题【HDOJ1421【HDOJ1058【HDOJ2546

    http://acm.hdu.edu.cn/showproblem.php?pid=2546

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=3361

    http://blog.sina.com.cn/s/blog_5123df350100e8p8.html

    http://blog.sina.com.cn/s/blog_51cea4040100gvn3.html

    http://blog.csdn.net/wukonwukon/article/details/6939050

    搬寝室

    Problem Description
    搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太多了,于是xhd决定随便搬2*k件过去就行了.但还是会很累,因为2*k也不小是一个不大于n的整数.幸运的是xhd根据多年的搬东西的经验发现每搬一次的疲劳度是和左右手的物品的重量差的平方成正比(这里补充一句,xhd每次搬两件东西,左手一件右手一件).例如xhd左手拿重量为3的物品,右手拿重量为6的物品,则他搬完这次的疲劳度为(6-3)^2 = 9.现在可怜的xhd希望知道搬完这2*k件物品后的最佳状态是怎样的(也就是最低的疲劳度),请告诉他吧.
     
    Input
    每组输入数据有两行,第一行有两个数n,k(2<=2*k<=n<2000).第二行有n个整数分别表示n件物品的重量(重量是一个小于2^15的正整数).
    Output
    对应每组输入数据,输出数据只有一个表示他的最少的疲劳度,每个一行.


    先对a[]按小到大排序.设:dp[i][j]表示前 i 个物品中搬 j 对的最少疲劳度.

    显然,当i==2*j时,  dp[i][j]=dp[i-2][j-1] + (a[i]-a[i-1])^2
    因为,当   (a1-a2)^2+(a3-a4)^2 <=  (a1-a4)^2+(a3-a2)^2 ( a1<=a2<=a3<=a4 ).
    当i>2*j时,  dp[i][j]  =  min( dp[i-1][j] , dp[i-2][j-1]+(a[i]-a[i-1])^2 )

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 int dp[2100][1100];
     5 int a[2100];
     6 int n,k;
     7 int pow2(int a) {return a*a;}
     8 int min2(int a,int b) {return a<b?a:b;}
     9 
    10 int cmp(const void* a,const void* b){
    11   return *(int*)a-*(int*)b;
    12 }
    13 
    14 int main(){
    15   int i,j,l;
    16   while(EOF != scanf("%d%d",&n,&k)){
    17     for(i=1;i<=n;i++) 
    18         scanf("%d",&a[i]);
    19     qsort(a+1,n,sizeof(a[0]),cmp);
    20     memset(dp,0,sizeof(dp));
    21     for(i=2;i<=n;i++){
    22       l=i/2;
    23       for(j=1;j<=l;j++){
    24         if(i==2*j)
    25             dp[i][j]=dp[i-2][j-1]+pow2(a[i]-a[i-1]);
    26         else 
    27             dp[i][j]=min2(dp[i-1][j],dp[i-2][j-1]+pow2(a[i]-a[i-1]));
    28       }
    29     }
    30     printf("%d
    ",dp[n][k]);
    31   }
    32   return 0;
    33 }
    Problem Description
    A number whose only prime factors are 2,3,5 or 7 is called a humble number.
    The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27,
    ... shows the first 20 humble numbers. 

    Write a program to find and print the nth element in this sequence
     1 #include <stdio.h>
     2 int f[5843],n;
     3 int i,j,k,l;
     4 
     5 int min(int a,int b,int c,int d){
     6     int min=a;
     7     if(b<min) min=b;
     8     if(c<min) min=c;
     9     if(d<min) min=d;
    10 
    11     if(a==min) i++;
    12     if(b==min) j++;
    13     if(c==min) k++;
    14     if(d==min) l++;
    15 
    16     return min;
    17 }
    18 
    19 int main(){
    20     i=j=k=l=1;
    21     f[1]=1;
    22     for(int t=2;t<=5842;t++)
    23         f[t]=min(2*f[i],3*f[j],5*f[k],7*f[l]);
    24 
    25     while(scanf("%d",&n)&&n!=0){
    26         if(n%10==1&&n%100!=11)
    27             printf("The %dst humble number is %d.
    ",n,f[n]);
    28         else if(n%10==2&&n%100!=12)
    29             printf("The %dnd humble number is %d.
    ",n,f[n]);
    30         else if(n%10==3&&n%100!=13)
    31             printf("The %drd humble number is %d.
    ",n,f[n]);
    32         else
    33             printf("The %dth humble number is %d.
    ",n,f[n]);
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    Qt之qInstallMessageHandler(重定向至文件)
    linux下MySQL安装及设置
    Apache2 同源策略解决方案
    BSD和云 – 不可错过的BSD聚会
    Nginx转发地址解决跨域问题
    Nginx下css的链接问题
    nginx 基本操作
    Azure 媒体服务可将优质内容传输至 Apple TV
    支付宝接口
    drf过滤组件
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/3621932.html
Copyright © 2011-2022 走看看