题意:给你一个礼物的置换。有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. Linux命令(2):cat

    cat命令是一个文本连接和查看的命令,用于文件的输出显示. 三大功能 一次显示整个文件. $ cat filename 从键盘创建一个文件.只能创建新文件,不能编辑已有文件. $ cat > f ...

  2. jQuery 基本使用

    index.html <head><meta http-equiv="Content-Type" content="text/html; charset ...

  3. AtCoder ABC 127F Absolute Minima

    题目链接:https://atcoder.jp/contests/abc127/tasks/abc127_f 题目大意 初始状态下$f(x) = 0$,现在有 2 种模式的询问,第一种以“1 a b” ...

  4. 2019 IEEEXtreme 13.0 Impact Factor 影响因子

    Impact Factor 题目大意 顾名思义,求影响因子.有 n 行 json 数据,第一行为期刊信息,后面为出版商出版的文章信息.   输入输出 点击查看详细 输入: 6 {"publi ...

  5. css中图片有缩放和转动效果

    现在html中利用div来包裹住一张图片. <div class="xuanzhuan"> <img src="images/top.png" ...

  6. spark自定义函数之——UDAF使用详解及代码示例

    UDAF简介 UDAF(User Defined Aggregate Function)即用户定义的聚合函数,聚合函数和普通函数的区别是什么呢,普通函数是接受一行输入产生一个输出,聚合函数是接受一组( ...

  7. SSM 整合 Shiro

    1. 导包 <!-- spring --> <dependency> <groupId>org.springframework</groupId> &l ...

  8. 零基础入门学习python--第一章

    知识点汇总1. Python的应用范围:操作系统.3D动画.WEB.企业应用.云计算等.2. Python是什么类型的语言?脚本语言,即电脑编程语言,比C.C++或java之类的系统编程语言简单容易. ...

  9. I Love Palindrome String HDU - 6599 回文树+hash

    题意: 输出每个长度下的回文串(这些回文串的左半边也需要是回文串) 题解: 直接套上回文树,然后每找到一个回文串就将他hash. 因为符合要求的回文串本身是回文串,左半边也是回文串,所以它左半边也右半 ...

  10. java 打印1到n之间的整数

    package java_day08; /* * 打印1-n之间的整数 * * 递归:方法体自己调用自己 */ public class DiGui { public static void main ...