这道题看隔壁卢姥爷在做,然后就跟着做了
题意
解释
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define int long long
#define maxn 1010
#define maxm 5010
using namespace std ;
int n , ans = 1e9 , c[maxn+5] , f[maxn+5][2*maxm+5] ;
signed main () {
cin >> n ;
for(int i = 1 ; i <= n ; i ++) {
int up , down ;
cin >> up >> down ;
c[i] = up - down ;
}
memset(f,0x3f,sizeof(f)) ;
f[0][maxm] = 0 ;
for(int i = 1 ; i <= n ; i ++) {
for(int j = -maxm ; j <= maxm ; j ++) {
f[i][j+maxm] = min(f[i-1][j-c[i]+maxm],f[i-1][j+c[i]+maxm] + 1 ) ;
}
}
for(int i = 0 ; i <= maxm ; i ++) {
ans = min(f[n][-i+maxm],f[n][i+maxm]) ;
if(ans < n) {
cout << ans <<"
" ;
break ;
}
}
return 0 ;
}
溜了溜了!