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. C#、Java和JS实现SHA256+BASE64加密总结

    C#.Java和JS实现SHA256+BASE64加密总结 --莫非(www.muphy.me) 原理 首先,通过编码格式(UTF-8.ASCII等,如果含有汉字等字符,编码格式不同加密结果也不同)获 ...

  2. 【洛谷P1313 计算系数】

    题目连接 #include<algorithm> #include<iostream> #include<cstring> #include<cstdio&g ...

  3. linux device drivers ch02

    ch02.构造和运行模块 模块的构造: #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(&qu ...

  4. zkclient中包引用不对,导致NoSuchMethodError

    nidonglin commented on 31 Oct 2014 Exception in thread "main" java.lang.NoSuchMethodError: ...

  5. 使用SO_REVTIMEO套接字选项为recvfrom设置超时

    void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int n; ]; struct timeval ...

  6. 全卷积网络 FCN 详解

    背景 CNN能够对图片进行分类,可是怎么样才能识别图片中特定部分的物体,在2015年之前还是一个世界难题.神经网络大神Jonathan Long发表了<Fully Convolutional N ...

  7. Rich feature hierarchies for accurate object detection and semantic segmentation(理解)

    0 - 背景 该论文是2014年CVPR的经典论文,其提出的模型称为R-CNN(Regions with Convolutional Neural Network Features),曾经是物体检测领 ...

  8. 设计模式八: 委派(Delegate)

    简介 委派模式不属于GOF23种设计模式, 主要角色有三种: 抽象任务角色, 委派者角色, 具体任务角色. 实现层面上, 定义一个抽象接口, 它有若干实现类, 他们真正执行业务方法, 这些子类是具体任 ...

  9. Queue的相关API

    public interface Queue<E> extends Collection<E> :队列通常是以FIFO(先进先出)方式排序元素. boolean add(E e ...

  10. Mac环境下的mongodb的安装

    1.安装MongoDB brew install mongodb 这个是默认安装最新版本的 mogodb,如果想安装指定版本可以先查看 mongodb 版本 brew search mongodb m ...