【题目链接】: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. WebView播放H5课件时,锁屏解锁后,页面重新绘制的问题

    难题描述:H5页面播放 ,锁屏,解锁后,重新加载了页面,三星不会出现(onpause onstop ,onresume),但在小米.魅族会调用 onpause onstop ondestroy,onr ...

  2. maven 国内完整源

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  3. Sublime Text 汉化插件

    https://blog.csdn.net/heyangyi_19940703/article/details/51869502 一.Sublime Text工具介绍: Sublime Text 是一 ...

  4. codevs1574广义斐波那契数列

    1574 广义斐波那契数列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond     题目描述 Description 广义的斐波那契数列是指形如an=p* ...

  5. mysql select 操作优先级

    单表查询操作 select filed1,filed2... form table where ... group by ... having .... order by ... limit ... ...

  6. Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

    总算是弄出来了,先写下来供自己以后查阅. 1)首先你要有一个阿里云服务器,我用的是Centos7学生认证,10元/月,很便宜也很好用. 2)购买了域名,首年9元,很划算.域名买来之后经历了拍照备案,前 ...

  7. ACM_情人节

    情人节 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某发每天都在各大群水啊水,然后认识了很多崇拜他的妹子,毕竟是数学专业.这不 ...

  8. Coursera公开课-Machine_learing:编程作业8(2016-10-06 20:49)

    Anomaly Detection and Recommender Systems 本周编程作业分为两部分:异常检测和推荐系统. 异常检测:本质就是使用样本的到特种值的gaussian分布,来预估正确 ...

  9. 实现strcpy

    #include <stddef.h> char* strcpy(char* dest, const char* src) { if (dest == NULL || src == NUL ...

  10. linux,apache,mysql,php常用查看版本信息的方法

    1. 查看linux的内核版本,系统信息,常用的有三种办法: uname -a; cat /proc/version; -bash-4.2$ uname -a Linux apphost -.el7. ...