题意:给你一个礼物的置换。有k个人忘带了礼物。一个人无法获得礼物为他自己没有带礼物或给他带礼物的那个人没有带礼物。求选择k个人,没有获得礼物的人数的最小值和最大值。

n,k<=1e6.

标程:

 #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int read()
{
int x=,f=;char ch=getchar();
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<='') x=(x<<)+(x<<)+ch-'',ch=getchar();
return x*f;
}
const int inf=0x3f3f3f3f;
const int N=;
int n,k,m,sum,tot,cnt,vis[N],cir[N],dp[N],a[N],Max,Min,len,v[N],t;
int main()
{
n=read();k=read();sum=n;
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<=n;i++)
if (!vis[i])
{
cnt=;
for (int j=a[i];j!=i;j=a[j]) cnt++,vis[j]=;
cir[++tot]=cnt;sum-=(cnt&);
}
sort(cir+,cir+tot+);
if (k*<=sum) Max=k*;else Max=min(sum+k-sum/,n); for (int i=,j;i<=tot;i++)
if (cir[i]!=cir[i+])
{
len++;
for (j=;len;j<<=) t=min(len,j),v[++m]=t*cir[i],len-=t;
}else len++;
dp[]=;
for (int i=;i<=m;i++)
for (int j=k;j>=v[i];j--)
dp[j]=dp[j]|dp[j-v[i]];
Min=k+(!dp[k]);
printf("%d %d\n",Min,Max);
return ;
}

题解:贪心+背包

提取若干个独立的礼物置换群。

对于一个置换:选k个人的最小值是k+1,最大值是2k。出界了和环长取min。

那么对于所有的置换,最大值就是每个环中尽可能两个两个取,没有两个可取了就一个一个取。

最小值,如果k恰好能表示成若干个环长的和,那么答案就是k,反之会有多余,是k+1。用多重背包算一下能否装满k个即可。用二进制优化做到nlogn*n^0.5(不同的环长约有n^0.5个)。(当然还有单调队列优化,可惜我不会)

CF755F PolandBalls and Gifts的更多相关文章

  1. Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包

    PolandBall and Gifts 转换成置换群后, 对于最大值我们很好处理. 对于最小值, 只跟若干个圈能否刚好组能 k 有关. 最直观的想法就是bitset优化背包, 直接搞肯定T掉. 我们 ...

  2. Codeforces 229E Gifts 概率dp (看题解)

    Gifts 感觉题解写的就是坨不知道什么东西.. 看得这个题解. #include<bits/stdc++.h> #define LL long long #define LD long ...

  3. Codeforces Round #357 (Div. 2) D. Gifts by the List 水题

    D. Gifts by the List 题目连接: http://www.codeforces.com/contest/681/problem/D Description Sasha lives i ...

  4. codeforces 755F F. PolandBall and Gifts(贪心+多重背包)

    题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes in ...

  5. 【OpenJ_Bailian - 4110】圣诞老人的礼物-Santa Clau’s Gifts (贪心)

    圣诞老人的礼物-Santa Clau’s Gifts  Descriptions: 圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖果有自己的价值和重量,每箱糖果都可以拆分成 ...

  6. Codeforces 755 F. PolandBall and Gifts 多重背包+贪心

    F. PolandBall and Gifts   It's Christmas time! PolandBall and his friends will be giving themselves ...

  7. 【33.33%】【codeforces 681D】Gifts by the List

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. (全国多校重现赛一)B-Ch's gifts

    Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing a ...

  9. Custom LED Keychain, Small And Surefire Gifts

    The    LED Keychain    makes it easy for people to carry their keys with them and carry them with th ...

随机推荐

  1. 互联网公司java面试题(一)

    1.JDK和JRE区别? JDK是整个JAVA的核心,包括了Java运行环境JRE,一堆Java工具和Java基础的类库.通过JDK开发人员将源码文件(java文件)编译成字节码文件(class文 件 ...

  2. 数据结构C++版-树

    一.概念 树是节点的有限集合. 二叉树: 二.补充知识点 1.<二叉树编码实战二>课程笔记: 递归的基本概念:程序调用自身的编程技巧称为递归,是函数自己调用自己. 迭代:利用变量的原值推算 ...

  3. 3、发送第一个api请求

    接口地址:https://www.v2ex.com/api/topics/latest.json Method: GET Authentication: None 我们打开postman,方法选择ge ...

  4. 20140415 HOG 不同继承方式的访问特性 虚函数工作原理

    1.HOG block重叠的好处 由于行人通常其形状可以视为柔体,人 的边缘位置不固定,而有一些移动,block 重叠后,一个边缘的梯度信息在两个相邻重叠的 block 中都能有所表达,这样即使边缘的 ...

  5. Day 7 :一句话Python(匿名函数-lambda,三元运算,列表表达式,生成器表达式)

    注意: 1.所有的列表表达式都可以转换成生成器表达式 2.经量让标傲世简化你得操作,增加代码可读性 3.如果代码过于复杂,应该转换成普通代码 4.再代码中尽可能多使用生成器表达式. 三元运算符:简化代 ...

  6. python学习7—函数定义、参数、递归、作用域、匿名函数以及函数式编程

    python学习7—函数定义.参数.递归.作用域.匿名函数以及函数式编程 1. 函数定义 def test(x) # discription y = 2 * x return y 返回一个值,则返回原 ...

  7. sklearn提供的自带数据集

    sklearn 的数据集有好多个种 自带的小数据集(packaged dataset):sklearn.datasets.load_<name> 可在线下载的数据集(Downloaded ...

  8. iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 5000 -j DNAT --to-destination

    启动docker容器时报错: iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 5000 -j DNAT ...

  9. jsonArray转换成List

    从字符串String转换成List 字符串格式: String jsonstr = "{'studentsjson':[{'student':'张三'},{'student':'李四'}] ...

  10. yolo3使用darknet卷积神经网络训练pascal voc

    darknet本来最开始学的是https://github.com/pjreddie/darknet yolo3作者自己开发的,但是它很久不更新了而且mAP值不好观察,于是另外有个https://gi ...