Codeforces 755F 规模较大的装箱dp
来源:互联网 发布:泰国有4g网络吗 编辑:程序博客网 时间:2024/05/16 08:40
http://codeforces.com/problemset/problem/755/F
max只要仔细一点就好了,关键是求min
1.因为送礼数组是1-n的排列,所以首先把所有送礼的圈找出来。
2.简单贪心后,min只有可能是K或者K+1,到底是哪一个就看有没有 某些圈的size和刚好是K
3.然后好像就变成了一个(1e6*1e6)的装箱dp。就算bitset优化复杂度也在1e10以上。
4.题解就是一个套路: size大的圈bitset优化做,size小的中一样的放一起做多重背包(当然这个多重背包要是O(k)的)……
5.玄学……感受一下学个思想吧
6.还有分块标准也不总是sqrt,居然选什么做临界要看复杂度的表达式……
#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <set>#include <map>#include <queue>#include <algorithm>#include <stack>#include <cctype>#include <cmath>#include <vector>#include <sstream>#include <bitset>#include <deque>#include <iomanip>using namespace std;#define pr(x) cout << #x << " = " << x << endl;#define bug cout << "bugbug" << endl;#define ppr(x, y) printf("(%d, %d)\n", x, y);#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define SQR(a) ((a)*(a))#define PCUT puts("\n---------------")typedef long long ll;typedef double DBL;typedef pair<int, int> P;typedef unsigned int uint;const int MOD = 1e9 + 7;const int inf = 0x3f3f3f3f;const ll INF = 0x3f3f3f3f3f3f3f3f;const int maxn = 1e6 + 4;const int maxm = 1e2 + 4;const double pi = acos(-1.0);int n, K;int a[maxn];bool vis[maxn]; vector<int> sz;int cnt[maxm], lst[maxm];bitset<maxn> dp;int main(){//必须编译过才能交int ik, i, j, k, kase;scanf("%d%d", &n, &K);for (i = 1; i <= n; ++i) scanf("%d", a+i);for (i = 1; i <= n; ++i)if (!vis[i]){int cnt = 0;int beg = i;do{cnt++;beg = a[beg];vis[beg] = true;}while(beg != i);sz.push_back(cnt);}int maxv = 0;if (K <= sz.size()) maxv = 2 * K;else if (K >= n - sz.size()) maxv = n;else{int tot = 0, res = 0;for (i = 0; i < sz.size(); ++i) tot += sz[i] - sz[i] % 2, res += sz[i]&1; if (tot >= K*2) maxv = K * 2;else if (res >= (K-tot/2)) maxv = tot + K - tot/2;else maxv = n;}dp.reset();dp[0] = 1;for (i = 0; i < sz.size(); ++i)if (sz[i] >= 100)dp |= dp << sz[i];else cnt[sz[i]]++;for (i = 2; i < 100; ++i)if (cnt[i]){memset(lst, 0x3f, sizeof lst);for (j = 0; j <= K; ++j){int index = j % i;if (dp[j]) lst[index] = j;else if (lst[index] != inf && (j - lst[index]) / i <= cnt[i]) dp[j] = true;}}int minv = dp[K] ? K : K + 1;printf("%d %d\n", minv, maxv);return 0;}
0 0
- Codeforces 755F 规模较大的装箱dp
- CodeForces 120F - 简单的树形DP
- 【Codeforces 500F】Dp
- 规模较大的C++项目代码层次结构
- 规模较大的C++项目代码层次结构
- codeforces 234F Fence (dp)
- codeforces 567F mausoleum dp
- Codeforces 234F (dp)
- 树形dp-CodeForces 581F
- codeforces 120F Spiders 树形DP
- CodeForces - 489F Special Matrices(DP)
- Special Matrices - CodeForces 489 F dp
- codeforces 489F Special Matrices(DP)
- codeforces 490F Treeland Tour(dp)
- codeforces 509F Progress Monitoring (区间dp)
- Codeforces 490F Treeland Tour(dp)
- Progress Monitoring - CodeForces 509 F dp
- Codeforces 474D (五一训练 F)+DP
- Android studio异常Class not found using the boot class loader; no stack available
- 重写与重载编译时运行时不同之处
- 筛选需要的log信息保存到本地
- 通达OA触发器--压缩保存工作流中的表单文件和所有附件到服务器的指定目录(2013adv,2015,2016)
- 设置互斥锁的类型
- Codeforces 755F 规模较大的装箱dp
- OPC之DCOM配置
- ButterKnife v6.1.0的, v8.0.1主要的不同在以下几个关键词:
- 听讲座
- SpringMVC注解@RequestParam全面解析
- 浅谈信息安全及解决方案
- Java之多线程的实现:
- https ssl
- vue.js--Tab选项卡