hdu 1421 1159 1087 1160 5366 1257 light OJ 1110 uva 562 简单dp

hdu 1421

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
int a[2005];
int dp[2005][10005];

int main(){
    int n,m;
    while(cin >> n >> m){

        for(int i = 1;i <= n;i++){
            scanf("%d",&a[i]);
        }
        sort(a+1,a+n+1);

        for(int i = 0;i <= m;i++){
            dp[0][i] = INF;
            dp[1][i] = INF; 
        }
        for(int i = 0;i <= n;i++){
            dp[i][0] = 0;
        }
        for(int i = 2;i <= n;i++){
            for(int j = 1;j <= m;j++){
                if(i < 2*j){
                    dp[i][j] = INF;
                }   
                else{
                    dp[i][j] = min(dp[i-2][j-1]+(a[i-1]-a[i])*(a[i-1]-a[i]),dp[i-1][j]);
                }
            }
        }
        printf("%d\n",dp[n][m]);
    } 
    return 0;
}

hdu 1159

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
char a[1005];
char b[1005];
int dp[1005][1005];

int main(){
    while(scanf("%s %s",a+1,b+1)!=EOF){
        int len1 = strlen(a+1);
        int len2 = strlen(b+1);
        memset(dp,0,sizeof(dp));
        for(int i = 1;i <= len1;i++){
            for(int j = 1;j <= len2;j++){
                if(a[i] == b[j]){
                    dp[i][j] = dp[i-1][j-1]+1;
                }
                else{
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        printf("%d\n",dp[len1][len2]);
    }     
    return 0;
}

hdu 1087

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
int a[1005];
LL dp[1005];

int main(){
    int t;
    while(cin >>t ,t){
        for(int i = 0;i < t;i++){
            scanf("%d",&a[i]);
        }
        memset(dp,0,sizeof(dp));
        dp[0] = a[0];
        LL  ans = a[0];
        for(int i = 0;i < t;i++){
            dp[i] = a[i];
            for(int j = 0;j < i;j++){
                if(a[i] > a[j]){
                    dp[i] = MAX(dp[i],dp[j]+a[i]);
                }
            }
            ans = MAX(dp[i],ans);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

hdu 1160

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
struct node{
    int x,y,i;
    bool operator < (const node& a)const{
        return x < a.x;
    }
};
node a[100000];
int dp[100000];
void dfs(int now,int d){
    for(int i = now-1;i >= 0;i--){
        if(dp[i] == d-1 && a[i].x < a[now].x  && a[i].y > a[now].y){
            dfs(i,d-1);
            break;
        }
    }
    printf("%d\n",a[now].i);
}
int main(){
    int n = 0;
    while(scanf("%d%d",&a[n].x,&a[n].y)!=EOF) a[n].i = n+1,n++;
    sort(a,a+n);
    for(int i = 0;i < n;i++){
        dp[i] = 1;
    }
    int ma = 0;
    int ss ;
    for(int i = 0;i < n;i++){
        for(int j = 0;j < i;j++){
            if(a[i].y < a[j].y && a[i].x > a[j].x){
                dp[i] = max(dp[i],dp[j]+1);
            }
        }
        if(ma < dp[i]){
            ma = dp[i];
            ss = i;
        }
    }
    printf("%d\n",ma);
    dfs(ss,ma);
    return 0;
}

hdu 1231

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
int a[10005];
int dp[10005];
int l[10005];

int main(){
    int n;
    while(scanf("%d",&n)!=EOF&&n){
        int now = 0;
        int ans = 0;
        int flag = 0; 
        for(int i = 0;i < n;i++){
            scanf("%d",&a[i]);
            if(a[i] >= 0){
                flag = 1;
            }
        }
        int loc = 0;
        for(int i = 0;i < n;i++){
            if(a[i] > 0){
                now += a[i];
            }
            else{
                if(now + a[i] >= 0){
                    now += a[i]; 
                }
                else{
                    now = 0;
                    loc = i+1;
                }
            }
            ans = max(ans,now);
            dp[i] = ans;
            l[i] = loc;
        }
        printf("%d ",ans);
        if(flag == 0){
            printf("%d %d\n",a[0],a[n-1]);
        }
        else{
            int i = 0;
            if(ans!=0)
                for(i = 0;i < n;i++){
                    if(dp[i] == ans ){
                        break;
                    }
                }
            else{
                for(i = 0;i < n;i++){
                    if(a[i] == 0) break;
                }
            }
            printf("%d %d\n",a[l[i]],a[i]);                     
        }
    }
    return 0;

}

:另附一份简短的代码

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>

using namespace std;

int a[11000],s,M,L,l,R,N;

int main()
{
    while (scanf("%d",&N),N){
        s=0; M=INT_MIN; L=1; l=1;
        for (int j=1;j<=N;j++){
            scanf("%d",&a[j]);
            s+=a[j];
            if (s>M) L=l,M=s,R=j;   
            if (s<0) s=0,l=j+1;      
        }
        if (M<0){
            M=0; L=1; R=N;
        }
        printf("%d %d %d\n",M,a[L],a[R]);
    }
}

hdu 5366

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)

unsigned long long  f[100];

int main(){
    int n;
    f[1] = 1;
    f[2] = 2;
    f[3] = 3;
    f[4] = 5;
    f[5] = 8;
    for(int i = 6;i <= 60;i++){
        f[i] = f[i-3] + f[i-4] + f[i-5] + 3 + f[i-3];
    }
    while(cin >> n){
        cout << f[n] << endl;
    }
    return 0;

}

light OJ 1110

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
char a[1000];
char b[1000];
int dp[105][105];
string ans[105][105];
int main(){
    int t;
    cin >> t;
    int T = 1;
    while(t--){
        scanf("%s%s",a+1,b+1);
        int lena = strlen(a+1);
        int lenb = strlen(b+1);
        memset(dp,0,sizeof(dp));
        for(int i = 0;i <= lena ;i++){
            for(int j = 0;j <= lenb;j++){
                ans[i][j] = "";
            }
        }
        for(int i = 1;i <= lena;i++){
            for(int j = 1;j <= lenb;j++){
                if(a[i] == b[j]){
                    dp[i][j] = dp[i-1][j-1]+ 1;
                    ans[i][j] = ans[i-1][j-1] + a[i];
                }
                else{
                    if(dp[i-1][j] == dp[i][j-1]){
                        ans[i][j] = min(ans[i-1][j],ans[i][j-1]); 
                    }
                    else if(dp[i-1][j] > dp[i][j-1]){
                        ans[i][j] = ans[i-1][j]; 
                    }
                    else{
                        ans[i][j] = ans[i][j-1]; 
                    }
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        if(ans[lena][lenb].length()!=0)
            cout << "Case " << T++ <<": " << ans[lena][lenb] << endl;
        else
            cout << "Case " << T++ <<": " << ":(" << endl; 
    } 
    return 0;
}

hdu 1257

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)

int main(){
    int t;
    while(cin >> t){
        vector<int> vec;
        for(int i = 0;i < t;i++){
            int x;
            scanf("%d",&x);
            int flag  = 0;
            int mi = INF;
            for(int j = 0;j < vec.size();j++){
                if(vec[j] >= x){
                    vec[j] = x;
                    flag = 1;
                    break;
                }               
            }
            if(!flag){
                vec.push_back(x);
            }
        }
        printf("%d\n",vec.size());
    }
    return 0;
}

uva 562

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int a[105];
int dp[105][50005];

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        int num = 0,sum = 0;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
        }
        num = sum / 2; 
        memset(dp,0,sizeof(dp));
        for(int i = 0; i <= n; i++)
        {
            dp[i][0] = 1;
        }
        for(int i = 1 ; i <= n;i++)
        {

            for(int j = 1; j <= num; j++)
            {
                if(j >= a[i])
                {

                    dp[i][j] = dp[i-1][j] | dp[i-1][j-a[i]];
                }
                else
                {
                    dp[i][j] = dp[i-1][j];
                }

            }
        }
        for(int i = num; i >= 0; i --)
        {
            if(dp[n][i])
            {
                printf("%d\n",sum - 2 * i);
                break;
            }
        }

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值