| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
我来贴一个比较好想的程序:#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <deque>
#include <locale>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include <wctype.h>
#include <algorithm>
#include <bitset>
#include <map>
#include <iomanip>
#include <ios>
#include <iostream>
#include <vector>
#include <cwchar>
#include <cwctype>
#define mp make_pair
#define fs first
#define se second
#define memset(a,t) memset(a,t,sizeof(a))
#define all(v) v.begin(),v.end()
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define MN 0LL
#define MX 20000000000000005
#define Mx 200000005
using namespace std;
int dp[1005][12005],n;
pair<int,int> a[1005];
int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin>>n;
int i,j;
for(i=1;i<=n;i++) cin>>a[i].first>>a[i].second;
for(i=0;i<1005;i++) for(j=0;j<12005;j++) dp[i][j]=Mx;
dp[0][6000]=0;
for(i=1;i<=n;i++){
for(j=0;j<12005;j++){
if(dp[i-1][j]!=Mx){
if(j+a[i].first-a[i].second<12005&&j+a[i].first-a[i].second) dp[i][j+a[i].first-a[i].second]=min(dp[i-1][j],dp[i][j+a[i].first-a[i].second]);
if(j-a[i].first+a[i].second<12005&&j-a[i].first+a[i].second) dp[i][j-a[i].first+a[i].second]=min(dp[i-1][j]+1,dp[i][j-a[i].first+a[i].second]);
}
}
}
int ans=0,wz;
for(i=6000;i<12005;i++){
if(dp[n][i]!=Mx){
ans=dp[n][i];
wz=abs(i-6000);
break;
}
}
for(i=6000;i>-1;i--){
if(dp[n][i]!=Mx){
if(abs(6000-i)<wz){
cout<<dp[n][i]<<endl;
return 0;
}
break;
}
}
cout<<ans<<endl;
return 0;
#ifdef home
eprintf("time = %d ms\n", (int)(clock() * 1000. / CLOCKS_PER_SEC));
#endif
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator