這題就是個揹包啊,感受是\(nms\)的可是不到0.2s,發生了什麼。。c++
就是設\(f[i]\)爲選了\(i\)我的最大的代價,而後有用的人數只有\(s\)種spa
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 500005 #define ba 47 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int64 f[20005]; int a[105][105],S,N,M; vector<int> v; vector<pii > vec; void upmax(int64 &x,int64 y) { x = max(x,y); } void Solve() { read(S);read(N);read(M); for(int i = 1 ;i <= S ; ++i) { for(int j = 1 ; j <= N ; ++j) { read(a[i][j]); } } for(int j = 1 ; j <= N ; ++j) { v.clear();vec.clear(); for(int i = 1 ; i <= S ; ++i) { if(a[i][j] * 2 + 1 <= M) v.pb(a[i][j] * 2 + 1); } sort(v.begin(),v.end()); if(v.size()) { vec.pb(mp(v[0],1)); for(int i = 1 ; i < v.size() ; ++i) { pii t = vec.back(); if(v[i] == v[i - 1]) { t.se++;vec.pop_back();vec.push_back(t); } else vec.pb(mp(v[i],t.se + 1)); } } for(int i = M ; i >= 0 ; --i) { for(auto t : vec) { if(i >= t.fi) { upmax(f[i],f[i - t.fi] + 1LL * t.se * j); } else break; } } } out(f[M]);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }