【codeforces 755F】PolandBall and Gifts
【题目链接】:http://codeforces.com/contest/755/problem/F
【题意】
n个人;
计划是每个人都拿一个礼物来送给一个除了自己之外的人;
且如果一个人没有送出礼物,那么它和它送礼物的对象都得不到礼物;
但是已经知道有k个人会忘记带礼物来;
问最少有几个人收不到礼物,最多有多少个人收不到礼物
【题解】
①
对于最多的情况;
最后会形成多个环;
每个环隔一个人选一个人忘记带礼物;这样一个长度为len的环只要len/2个人没带礼物就能够整个环的人都收不到礼物了;
然后如果len为奇数就放在最后再处理;因为那些多余的一个人必须得用1个名额来补充;所以先放在最后;因为你当前一个名额能够抵消两个人,肯定优先抵消两个人;
②
对于最少的情况;
就是k个名额刚好分配每个环的人数;
如果不是刚好;
则会多出一个人即k+1
那么问题就转换成一个背包问题了;
把每个环的长度看成一个物品,同种长度的环可能有多个
->多重背包;
问能不能刚好体积为k;
这里用到了多重背包的二进制优化;
然后还用到了bitset的技巧;
即把增加容量和
二进制的左移操作对应;
很厉害。。
【Number Of WA】
3
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e6+1000;
int n,k,a[N],b[N],num,cnt,ans1,ans2,w[N];
bool vis[N];
bitset<N> f;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
rei(n),rei(k);
rep1(i,1,n)
rei(a[i]);
rep1(i,1,n)
if (!vis[i])
{
num++,cnt = 0;
int j = i;
while (!vis[j])
{
vis[j] = true;
cnt++;
j = a[j];
}
b[num] = cnt;
}
n = num;
sort(b+1,b+1+n);
int rest = k,t = 0;
rep1(i,1,n)
{
if (b[i]/2<=rest)
{
rest-=b[i]/2;
ans2+=b[i]/2*2;
}
else
{
ans2+=rest*2;rest = 0;
break;
}
if (b[i]&1) t++;
}
ans2+=min(rest,t);
num = 0;
rep1(i,1,n)
{
int j = i;
while (j+1<=n && b[j+1]==b[i]) j++;
int t = 1,len = j-i+1;
while (len>=t)
{
w[++num] = t*b[i];
len-=t;
t<<=1;
}
if (len)
w[++num] = len*b[i];
i = j;
}
n = num;
f[0] = 1;
rep1(i,1,n)
f|=f<<(w[i]);
ans1 = k+1;
if (f[k])
ans1 = k;
pri(ans1<<' '<<ans2<<endl);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 755F】PolandBall and Gifts的更多相关文章
- 【codeforces 755B】PolandBall and Game
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 755A】PolandBall and Hypothesis
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755D】PolandBall and Polygon
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755C】PolandBall and Forest
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 755E】PolandBall and White-Red graph
[题目链接]:http://codeforces.com/contest/755/problem/E [题意] 给你n个节点; 让你在这些点之间接若干条边;构成原图(要求n个节点都联通) 然后分别求出 ...
- codeforces 755F F. PolandBall and Gifts(贪心+多重背包)
题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes in ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【27.85%】【codeforces 743D】Chloe and pleasant prizes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
随机推荐
- RxJava使用介绍
主讲人:阳石柏 RxJava基本概念 背压概念介绍 RxJava 2.0版本介绍及更新 一.RxJava基本概念 RxJava 在 GitHub 主页上的自我介绍是 “a library for co ...
- sql server数据库添加记录
转自:http://jingyan.baidu.com/article/f25ef254449a9a482c1b8293.html
- 【HNOI 2003】 激光炸弹
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1218 [算法] 二维前缀和 [代码] #include<bits/stdc++ ...
- [python基础] csv.wirterow()报错UnicodeEncodeError
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错,python没办法处理非ascii编码的,此时需要自己设置将python的默认编码,一 ...
- [Swift通天遁地]九、拔剑吧-(9)创建支持缩放、移动、裁切的相机视图控制器
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 利用Kibana来查看和管理Elasticsearch的索引(Kibana使用篇)
经过前面几篇的学习,我们已经知道如何在Kibana里边对Elasticsearch进行操作了,那么如何查看已存在的索引呢,我们来看, 在Management里边可以看到我们刚刚创建的sdb索引.另外还 ...
- $P5017 摆渡车$
problem 毒瘤\(DP\) #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef lon ...
- 【USACO2002 Feb】奶牛自行车队
[USACO2002 Feb]奶牛自行车队 Time Limit: 1000 ms Memory Limit: 131072 KBytes Description N 头奶牛组队参加自行车赛.车队在比 ...
- 修改docker-toolbox/boot2docker容器镜像
进入虚拟机 vi /var/lib/boot2docker/profile 编辑在EXTRA_ARGS,加入 --registry-mirror=https://pee6w651.mirror.ali ...
- Scala-基础-数据类型
import junit.framework.TestCase import org.junit.Test import scala.runtime.RichByte //数据类型 class Dem ...