【题目链接】: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的更多相关文章

  1. 【codeforces 755B】PolandBall and Game

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

  2. 【codeforces 755A】PolandBall and Hypothesis

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 755D】PolandBall and Polygon

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 755C】PolandBall and Forest

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

  5. 【codeforces 755E】PolandBall and White-Red graph

    [题目链接]:http://codeforces.com/contest/755/problem/E [题意] 给你n个节点; 让你在这些点之间接若干条边;构成原图(要求n个节点都联通) 然后分别求出 ...

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

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

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

随机推荐

  1. git subtree 拆分split repository

    subtree出现,是为了取代submodule http://wenku.baidu.com/link?url=ola85Z5tIXJpxCjLTk-dcO81ayXLs68_y6dsmXIa0ni ...

  2. Speed Limit

    http://poj.org/problem?id=2017 #include<stdio.h> int main() { int n,mile,hour; ) { ,h = ; whil ...

  3. 微信小程序压缩图片并上传到服务器(拿去即用)

    这里注意一下,图片压缩后的宽度是画布宽度的一半 canvasToTempFilePath 创建画布的时候会有一定的时间延迟容易失败,这里加setTimeout来缓冲一下 这是单张图片压缩,多张的压缩暂 ...

  4. T4模板使用记录,生成Model、Service、Repository

    自己目前在搭建一个.NET Core的框架,本来是打算使用前端做代码生成器直接生成到文件的,快做好了.感觉好像使用T4更方便一些,所以也就有了这篇文章~  我还是有个问题没解决,就是我想生成每个类(接 ...

  5. 【BZOJ1124】[POI2008]枪战Maf(基环树_构造)

    被教练勒令做题不能看题解后的第一道新题,自行 yy 了好久终于 AC 了(菜啊)--写博客纪念. 题目: BZOJ1124 分析: 考虑每个人向他要打的人连边.根据题意,所有点都有且只有一条出边.那么 ...

  6. ACM_Alien And Password

    Alien And Password Time Limit: 2000/1000ms (Java/Others) Problem Description: Alien Fred wants to de ...

  7. Coursera公开课-Machine_learing:编程作业7

    这周的编程作业主要是两方面内容. 1.K-means聚类. 2.PCA(Principle Component Analys)主成分分析. 方式主要是通过对图像的聚类实现压缩图像,后来发现PCA也可以 ...

  8. [转]Linux finger命令

    转自:http://os.51cto.com/art/201003/186354.htm Linux finger命令是系统管理员的必备命令之一,他可以清楚的告诉管理员有多少用户在同时使用他所管理的L ...

  9. fcc html5 css 练习2

    <form action="/submit-cat-photo" >action属性的值指定了表单提交到服务器的地址 <input type="text ...

  10. JS高级——扩展内置对象的方法

    基本概念 内置对象有很多,几个比较重要的:Math.String.Date.Array 基本使用 1.内置对象创建出来的对象使用的方法使用的其实都是内置对象的原型对象中的方法 (1)a并没有charA ...