E. Let's Go Rolling!

http://codeforces.com/problemset/problem/38/E

DP状态很容易想到,以后超long long 最大值的设定最好计算出一个边界, int_max的边界赋值 错误,导致WA那么多次。

View Code
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>

typedef __int64 int64;
//typedef long long int64;
const __int64 int_max = 10000000000000;
//#define int_max 0x3f3f3f3f

int64 f_min(int64 x,int64 y) {if(x<y)return x; else return y;}
int f_max(int x,int y) {if(x<y)return y; else return x;}
void swap(int&x,int&y) {x=x^y;y=x^y;x=x^y;}
int64 f_abs(int64 x) {return (x)>(0)?(x):(-x);}
int lowbit(int x) {return (x)&(-x);}
bool isdig(char ch) {return ch>='0' && ch<='9';}
//double f_min(double x,double y) {if(x<y)return x; else return y;}
//double f_max(double x,double y) {if(x<y)return y; else return x;}
using namespace std;

const int MM = 22222;
#define mod 10000000000
const double Pi = acos(-1.0);
const double lep = 1e-10;
const double inf = 1000000000.00;
#define debug puts("wrong")

#define lson rt<<1
#define rson rt<<1|1

int dx[8]={1,2,1,2,-1,-2,-1,-2};
int dy[8]={2,1,-2,-1,2,1,-2,-1};


int64 N,M, Q;
int NE;
struct Pos {
    int64 x,c;
    bool operator<(Pos a) const {
        return x<a.x;
    }
}p[MM];


void get_data() {
    int64 i,j,k;    
    for(i=1;i<=N;i++) {
        scanf("%I64d%I64d",&p[i].x,&p[i].c);
    }
    sort(p+1,p+N+1);
}
int64 dp[MM][2];

void solve() {
    int64 i,j,k,tmp,sum;
    dp[0][0]=dp[0][1]=0;
    dp[1][1]=p[1].c; dp[1][0]=int_max;
    for(i=2;i<=N;i++) {
        dp[i][0]=dp[i][1]=int_max;
        sum=0; tmp=1;
        for(j=i-1;j>0;j--) {
            sum=sum+(p[j+1].x-p[j].x)*tmp;
            dp[i][0]=f_min(dp[i][0],dp[j][1]+sum);
            if(p[j+1].x==p[j].x) while(1);
            tmp++;
        }
        dp[i][1]=f_min(dp[i-1][0],dp[i-1][1])+p[i].c;
//        printf("%I64d %I64d %I64d\n",i,dp[i][0],dp[i][1]);
    }
    int64 ans=f_min(dp[N][0],dp[N][1]);
    printf("%I64d\n",ans);
}

int main() {
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    while(scanf("%I64d",&N)!=EOF) get_data(),solve();
    return 0;
}

 

posted @ 2013-03-29 14:23  zhang1107  阅读(298)  评论(0编辑  收藏  举报