LOJ#3092. 「BJOI2019」排兵布阵
这题就是个背包啊,感觉是\(nms\)的但是不到0.2s,发生了什么。。
就是设\(f[i]\)为选了\(i\)个人最大的代价,然后有用的人数只有\(s\)种
#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();
}