S+1 DP1
A. 多重背包问题 II
- 用优先队列维护可以做到n2的时间复杂度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38signed main()
{
IOS;
// solve();
// return 0;
int n, m;
cin >> n >> m;
//cout << n << ' ' << m << endl;
for (int i = 1; i <= n; i++)
{
int v, w, s;
cin >> v >> w >> s;
//s = min(s, m / v);
deque<int> q;
//cout << "SS : " << v << ' ' << w << ' ' << s << endl;
for (int j = 0; j < v; j++)
{
q.clear();
int now = i & 1, pre = (i - 1) & 1;
for (int _w = j; _w <= m; _w += v)
{
while (!q.empty() && DP[pre][q.back()] + (_w - q.back()) / v * w <= DP[pre][_w])
{
q.pop_back();
}
while (!q.empty() && _w - q.front() > v * s)
{
q.pop_front();
}
q.push_back(_w);
DP[now][_w] = DP[pre][q.front()] + (_w - q.front()) / v * w;
}
}
}
cout << DP[n & 1][m] << endl;
return 0;
}
B. 找零升级版
- 利用二进制优化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34int n, m;
bitset<1000005> f;
int DP[2000005];
int _;
signed main() {
IOS;
cin >> n >> m;
for (int i = 1; i <= n ;i ++) {
int v, w;
cin >> v >> w;
int cnt = 1;
while (w >= cnt) {
DP[++_] = cnt * v;
w -= cnt;
cnt <<= 1;
}
if (w) {
DP[++_] = w * v;
}
}
f[0] = 1;
for (int i = 1; i <= _; i++) {
f |= (f << DP[i]);
}
for (int i = 1; i <= m; i++) {
if (f[i]) {
cout << 1;
} else {
cout << 0;
}
}
//cout << endl;
return 0;
}
C. 最佳损友
- 就是用DP求前两个人可能的结果,再去算第三个人
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int DP[MAX][MAX];
signed main() {
IOS;
int n;
cin >> n;
vector<int> a(n + 1);
int sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
DP[0][0] = 1;
for (int i = 1; i <= n; i++) {
int x = a[i];
for (int j = sum; j >= 0; j--) {
for (int k = sum; k >= 0; k--) {
if (j >= x) {
DP[j][k] |= DP[j - x][k];
}
if (k >= x) {
DP[j][k] |= DP[j][k - x];
}
}
}
}
int ans = 1e9;
for (int j = 0; j <= sum; j++) {
for (int k = j; k <= sum; k++) {
if (!DP[j][k]) {
continue;
}
int tmp = sum - k- j;
if (tmp >= k) {
ans = min(ans, tmp);
}
}
}
cout << ans << endl;
return 0;
}
E. 不降数
- 计数DP的最基础的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38int dp[50][12];
int num[50];
int dfs(int pos, int pre, bool limit)
{
if (pos == 0)
return 1;
if (!limit && dp[pos][pre] != -1)
return dp[pos][pre];
int up = limit ? num[pos] : 9;
int res = 0;
for (int i = pre; i <= up; ++i)
res += dfs(pos - 1, i, limit && (i == num[pos]));
if (!limit)
dp[pos][pre] = res;
return res;
}
int solve(int x)
{
memset(dp, -1, sizeof(dp));
int cnt = 0;
while (x)
{
num[++cnt] = x % 10;
x /= 10;
}
return dfs(cnt, 0, 1);
}
signed main()
{
IOS;
int u, v;
while (cin >> u >> v)
{
cout << solve(v) - solve(u - 1) << endl;
}
return 0;
}
F. B数
- 计数DP的小进阶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
int DP[MAX][MAX][MAX][MAX];
int n;
int num[MAX];
int cnt = 0;
int INF = 1e18;
int dfs(int pos, int pre, int mod, int flag, int key) {
if (pos == 0) {
if (mod == 0 && flag == 1) {
return 1;
} else {
return 0;
}
}
if (!key && DP[pos][pre][mod][flag] != INF) {
return DP[pos][pre][mod][flag];
}
int maxn;
if (key == 0) {
maxn = 9;
} else {
maxn = num[pos];
}
int res = 0;
for (int i = 0; i <= maxn; i++) {
res += dfs(pos - 1, i, (mod * 10 + i) % 13, flag == 1 || (pre == 1 && i == 3), key && maxn == i);
}
if (key == 0) {
DP[pos][pre][mod][flag] = res;
}
return res;
}
signed main() {
IOS;
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
for (int k = 0; k < MAX; k++) {
DP[i][j][k][0] = INF;
DP[i][j][k][1] = INF;
}
}
}
int n;
cin >> n;
while (n) {
num[++cnt] = n % 10;
n /= 10;
}
cout << dfs(cnt, 0, 0, 0, 1) << endl;
//cout << INF << endl;
return 0;
}
G 平衡数
- 用三进制去表示状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72ll a,b;
int num[24];
int tri[12];
ll f[24][59049];
void init()
{
tri[0]=1;
for(int i=1;i<=10;i++)
{
tri[i]=tri[i-1]*3;
}
}
int add(int x,int y)//num x,pos y(tms++)
{
int tmp=x%tri[y+1]/tri[y],c;
if(tmp==0)
{
c=y%2+1;
}
else
{
c=tmp%2+1;
}
return x+(c-tmp)*tri[y];
}
bool check(int sta)
{
while(sta)
{
if(sta%3==2)return 0;
sta/=3;
}
return 1;
}
ll dfs(int x,int sta,bool limit,bool zer)
{
if(x==0)return check(sta);
if(!limit && !zer && f[x][sta]!=-1)return f[x][sta];
int up=limit?num[x]:9;
ll res=0;
for(int i=0;i<=up;i++)
{
if(zer && i==0 && x!=1)
{
res+=dfs(x-1,sta,limit&&(i==num[x]),zer&&(i==0));
}
else res+=dfs(x-1,add(sta,i),limit&&(i==num[x]),zer&&(i==0));
}
if(!limit && !zer)f[x][sta]=res;
return res;
}
ll solve(ll x)
{
int tp=0;
while(x)
{
num[++tp]=x%10;
x/=10;
}
return dfs(tp,0,1,1);
}
int main()
{
memset(f,-1,sizeof(f));
init();
int T=1;
while(T--)
{
cin >> a >> b;
cout << solve(b)-solve(a-1)) << endl;
}
}
H 国民代表
- 排序后找先后集合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49const int mn=305,mod=998244353;
struct sd
{
int p,q;
}a[mn];
int f[mn][mn][mn];//f[i][j][k] i ppl pkd j with a minq=k
int n,k,ans;
bool cmp(sd x,sd y)
{
return x.p<y.p;
}
int main()
{
cin >> n >> k;
for(int i=1;i<=n;i++)
{
cin >> a[i].p;
}
for(int i=1;i<=n;i++)
{
cin >> a[i].q;
}
sort(a+1,a+n+1,cmp);
f[0][0][n+1]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=k;j++)
{
for(int l=1;l<=n+1;l++)
{
// f[i][j][l]=f[i-1][j][l];
if(a[i].q<l && j!=0)
{
f[i][j][l]+=f[i-1][j-1][l];
f[i][j][l]%=mod;
}
f[i][j][min(l,a[i].q)]+=f[i-1][j][l];
f[i][j][min(l,a[i].q)]%=mod;
}
// cerr<<f[i][j][a[i].q];
}
}
for(int i=1;i<=n+1;i++)
{
ans+=f[n][k][i];
ans%=mod;
}
cout << ans << endl;
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ysmmm的快乐小屋!