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 ...
随机推荐
- P1802 5倍经验日
P1802 5倍经验日 题目背景 现在乐斗有活动了!每打一个人可以获得5倍经验!absi2011却无奈的看着那一些比他等级高的好友,想着能否把他们干掉.干掉能拿不少经验的. 题目描述 现在absi20 ...
- c# ToString()格式大全(转)
stringstr1 =string.Format("{0:N1}",56789); //result: 56,789.0stringstr2 =str ...
- dict,list强制类型转换
单列集合中如果装的元素都是双列,那么可以通过dict()强制转换为字典 a=[(1,1),(2,2),(3,3)] print(dict(a)) #{1:1,2:2,3:3} b=[1,2,3] pr ...
- git学习记录1(本地库管理)
学习参考地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 本编随笔只是自己对 ...
- TortoiseGit配置私钥关联github
1.使用Git 命令行生成公钥和私钥 找到Git安装目录(我的安装目录是 D:\DevelopSoft\Git),打开git-bash.exe. 输入命令 回车 ssh-keygen -t rsa ...
- 使用豆瓣源安装python包
Python pip安装的原理就是从Python的官方源https://pypi.python.org/pypi下载到本地安装, 但是访问官方源网络不稳定,速度较慢,我经常会遇到一个问题,pip安装包 ...
- 纯css制作三级菜单
<!DOCTYPE html> <html> <head> <title>三级菜单</title> <meta charset=&qu ...
- 2018-8-10-WPF-使用-Direct2D1-画图-绘制基本图形
title author date CreateTime categories WPF 使用 Direct2D1 画图 绘制基本图形 lindexi 2018-08-10 19:16:53 +0800 ...
- PL SQL 12.0.7的安装及注册码,汉化包,连接Oracle远程数据库,中文乱码问题处理
首先,在官网下载PL SQL 的对应版本,本机是64位的就下载64位的,网址:https://www.allroundautomations.com/downloads.html#PLS 点击应用程序 ...
- jupyter|魔法函数问题| UsageError: Line magic function `%` not found
问题: jupyter notebook 使用魔法函数% matplotlib inline,报错:UsageError: Line magic function `%` not found 解决: ...