PolandBall and Gifts

转换成置换群后, 对于最大值我们很好处理。

对于最小值, 只跟若干个圈能否刚好组能 k 有关。

最直观的想法就是bitset优化背包, 直接搞肯定T掉。

我们能再发掘一些性质, 就是本质不能的圈的大小最多有sqrt(n)个,

因为1 + 2 + 3 ... + n = (n + 1) * n / 2

所以对于每个不同的数二进制优化一下就可以过啦。

感觉这种题就很有意思。。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, k, p[N], c[N], num[N], cnt;
bool vis[N];
bitset<> dp;
vector<int> oo; int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) scanf("%d", &p[i]);
for(int i = ; i <= n; i++) {
if(vis[i]) continue;
int u = i; cnt++;
while(!vis[u]) {
vis[u] = true;
c[cnt]++;
u = p[u];
}
}
int one = , two = ;
for(int i = ; i <= cnt; i++)
(c[i] & ) ? one++, two += c[i] / : two += c[i] / ;
int mnans = , mxans = ;
if(two >= k) mxans = * k;
else {
mxans += two * + (k - two);
chkmin(mxans, n);
}
dp[] = ;
for(int i = ; i <= n; i++) oo.push_back(c[i]);
sort(ALL(oo)); oo.erase(unique(ALL(oo)), oo.end());
for(int i = ; i <= n; i++) num[lower_bound(ALL(oo), c[i]) - oo.begin()]++;
for(int i = ; i < SZ(oo); i++) {
for(int j = ; ( << j) <= num[i]; j++) {
int val = ( << j) * oo[i];
num[i] -= << j;
dp |= dp << val;
}
if(num[i]) {
int val = num[i] * oo[i];
dp |= dp << val;
}
}
mnans = dp[k] ? k : k + ;
printf("%d %d\n", mnans, mxans);
return ;
} /*
*/

Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包的更多相关文章

  1. poj1014二进制优化多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53029   Accepted: 13506 Descri ...

  2. 51nod 1086 背包问题 V2(二进制优化多重背包)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1086 题解:怎么用二进制优化多重背包,举一个例子就明白了. ...

  3. POJ - 1276 二进制优化多重背包为01背包

    题意:直接说数据,735是目标值,然后3是后面有三种钱币,四张125的,六张五块的和三张350的. 思路:能够轻易的看出这是一个多重背包问题,735是背包的容量,那些钱币是物品,而且有一定的数量,是多 ...

  4. [Bzoj 1192][HNOI2006]鬼谷子的钱袋(二进制优化多重背包)

    (人生第一篇bzoj题解有点激动 首先介绍一下题目: 看它题目那么长,其实意思就是给定一个数a,求将其拆分成n个数,通过这n个数可以表示出1~a中所有数的方案中,求最小的n. 您看懂了嘛?不懂咱来举个 ...

  5. BZOJ 1531 二进制优化多重背包

    思路: 讲道理我应该写单调队列优化多重背包的 但是我不会啊 但是我现在! 还不会啊 我就写了个二进制优化的.. 过了 //By SiriusRen #include <cstdio> #i ...

  6. 【二进制优化-多重背包】zznu-oj-2120 : 安详--如何用尽钱币打赏主播获得最大好感度

    2120 : 安详 题目描述 spring最近喜欢上了B站新秀主播,身为顿顿吃黄焖鸡的土豪,当然要过去打赏一番,但是spring还是喜欢精打细算,所以在打赏的时候,想要掏出有限的钱,获得主播的最大好感 ...

  7. poj 1742 Coins(二进制优化多重背包)

    传送门 解题思路 多重背包,二进制优化.就是把每个物品拆分成一堆连续的\(2\)的幂加起来的形式,然后把最后剩下的也当成一个元素.直接类似\(0/1\)背包的跑就行了,时间复杂度\(O(nmlogc) ...

  8. [tyvj-1194]划分大理石 二进制优化多重背包

    突然发现这个自己还不会... 其实也不难,就和快速幂感觉很像,把物品数量二进制拆分一下,01背包即可 我是咸鱼 #include <cstdio> #include <cstring ...

  9. [POJ1014]Dividing(二进制优化多重背包)

    #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int ...

随机推荐

  1. Nodejs的安装配置及如何在sublimetext2中运行js

    Nodejs的安装配置及如何在sublimetext2中运行js听语音 | 浏览:4554 | 更新:2015-06-16 11:29 Nodejs的安装配置及如何在sublimetext2中运行js ...

  2. sql 找出不包含字母、不包含汉字的数据

    --1.不包含字母 SELECT * FROM t WHERE str NOT LIKE '%[a-zA-Z]%' SELECT * FROM t --2.不包含汉字 SELECT * FROM t ...

  3. springcloud 新增微服务

    个人记录 记录公司微服务项目,模块添加的步骤 一  创建Module 选择maven groupid和artifactid 参考 pom文件 <project xmlns="http: ...

  4. leetcode-884两句话中的不常见单词

    ''' 给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有 ...

  5. C#开发Windows服务详细流程

    1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...

  6. c#根据文件路径启动进程

    //根据文件路径启动进程 private static void StartProcessByFilePath(string path) { Process p = new System.Diagno ...

  7. jS处理中英文时间格式化问题

    // datebox里面日期格式化,i18nStr是当前国际化的日期格式,fdate是需要处理的时间,默认为当前时间 function formatDate(i18nStr,fdate) { var ...

  8. mysql 分库分表 ~ 柔性事务

    一 定义 TCC方案是可能是目前最火的一种柔性事务方案二 具体 内容 TCC=try(预设)-confrim(应用确认)-canal(回滚取消)三 目的 解决跨服务调用场景下的分布式事务问题,避免使用 ...

  9. spring集成Junit做单元测试及常见异常解决办法

    spring-test依赖包 <!--Spring-test --> <!-- https://mvnrepository.com/artifact/org.springframew ...

  10. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...