CF755F PolandBalls and Gifts
题意:给你一个礼物的置换。有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的更多相关文章
- Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包
PolandBall and Gifts 转换成置换群后, 对于最大值我们很好处理. 对于最小值, 只跟若干个圈能否刚好组能 k 有关. 最直观的想法就是bitset优化背包, 直接搞肯定T掉. 我们 ...
- Codeforces 229E Gifts 概率dp (看题解)
Gifts 感觉题解写的就是坨不知道什么东西.. 看得这个题解. #include<bits/stdc++.h> #define LL long long #define LD long ...
- 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 ...
- codeforces 755F F. PolandBall and Gifts(贪心+多重背包)
题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes in ...
- 【OpenJ_Bailian - 4110】圣诞老人的礼物-Santa Clau’s Gifts (贪心)
圣诞老人的礼物-Santa Clau’s Gifts Descriptions: 圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖果有自己的价值和重量,每箱糖果都可以拆分成 ...
- Codeforces 755 F. PolandBall and Gifts 多重背包+贪心
F. PolandBall and Gifts It's Christmas time! PolandBall and his friends will be giving themselves ...
- 【33.33%】【codeforces 681D】Gifts by the List
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- (全国多校重现赛一)B-Ch's gifts
Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing a ...
- 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 ...
随机推荐
- 学习笔记——CDQ分治
再次感谢这位大佬的博客:https://www.cnblogs.com/ljc20020730/p/10395866.html CDQ分治,是一种在分治合并中计算前面值对后面答案的贡献的一种算法.今天 ...
- POJ 3761 Bubble Sort
题目链接:https://vjudge.net/problem/POJ-3761 转自:https://blog.csdn.net/cscj2010/article/details/7820906 题 ...
- 干货:Java正确获取客户端真实IP方法整理
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- Jupyter的使用复习
Jupyter的使用 esc+m 切换到markdown模式 shift+enter 运行 a 向上新增代码块 b 向下新增代码块 y python代码模式 file-->download as ...
- python接口自动化(接口基础)
一.什么是接口? 前端负责展示和收集数据 后端负责处理数据,返回对应的结果 接口是前端与后端之间的桥梁,传递数据的通道 二.
- MySQL sql_mode 说明(及处理一起sql_mode引发的问题)
转自:https://segmentfault.com/a/1190000005936172 1. MySQL 莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app ...
- if else 和 swith效率比较
读大话设计模式,开头的毛病代码用if else实现了计算器,说计算机做了三次无用功,优化后是用switch,那么switch为什么比if else效率高呢, 百度找了几个说是底层算法不一样,找了一个比 ...
- CIE XYZ
了解CIE XYZ的来龙去脉,看维基之前,先读这两篇文章: https://medium.com/hipster-color-science/a-beginners-guide-to-colorime ...
- C++ BASS 实例
#include <iostream> #include <string> #include <map> #include "..\sdk\bass\in ...
- thinkphp Trace方法
页面Trace只能用于有页面输出的情况,但是trace方法可以用在任何情况,而且trace方法可以用于AJAX等操作. Trace方法的格式: trace('变量','标签','级别','是否记录日志 ...