Codeforces-417D总结&题解
在分析题目前,一定要完全读懂题目意思,否则一些都是白搭。这道理谁都懂,但是能每次都做到的人不多。比如本人,这次又粗心地在这里疯狂地踩坑了。
(1)本题有这么一句话:But Gena's friends won't agree to help Gena for nothing:
the i-th
friend asks Gena xi rubles
for his help in solving all the problems he
can. 当时就没看清,然后,根据自己的一知半解去套样例,结果刚好符合我的理解(这也从侧面说明了一点,样例通常都是误导粗心大意的、naive的人):每个朋友,AC一题,交一次钱,如果当前已经买了的显示器足够这个朋友需要的,不用再额外支付显示器的钱,否则,还差多少个显示器,就要再付多少个显示器的钱。于是乎,就有了下面这份代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
typedef __int64 I64;
I64 n, m, b;
typedef struct {
I64 x, k, m;
set<I64> pl;
} Friend;
set<I64> st;
Friend fds[105];
I64 dp[22][105];
void init() {
st.clear();
for(I64 i = 0;i < 105;i++){
fds[i].x = 0, fds[i].k = 0, fds[i].m = 0;
fds[i].pl.clear();
}
}
void solve() {
memset(dp, 0x5f, sizeof(dp));
I64 t;
for(I64 i = 1; i <= n; i++)dp[0][i] = 0;
for(I64 i = 1; i <= n; i++){
if(!fds[i].pl.count(1))continue;
dp[1][i] = fds[i].x + fds[i].k * b;
//printf("dp[1][%I64d] = %I64d\n", i, dp[1][i]);
}
for(I64 p = 2; p <= m; p++) {
for(I64 j = 1; j <= n; j++) {
if(!fds[j].pl.count(p))continue;
for(I64 r = 1; r <= n; r++) {
//if(!fds[r].pl.count(p))continue;
if(dp[p - 1][r] == 0x5f5f5f5f5f5f5f5f)continue;
t = (fds[j].k > fds[r].k ? (fds[j].k - fds[r].k) * b : 0);
dp[p][j] = min(dp[p][j], dp[p - 1][r] + fds[j].x + t);
//printf("dp[%I64d][%I64d] = %I64d\n", p, j, dp[p][j]);
}
}
}
I64 res = dp[m][1];
for(I64 i = 1;i <= n;i++)res = min(res, dp[m][i]);
printf("%I64d\n", res);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Einput.txt", "r", stdin);
#endif // ONLINE_JUDGE
I64 p;
while(~scanf("%I64d%I64d%I64d", &n, &m, &b)) {
init();
for(I64 i = 1; i <= n; i++) {
scanf("%I64d%I64d%I64d", &fds[i].x, &fds[i].k, &fds[i].m);
for(I64 j = 0; j < fds[i].m;j++) {
scanf("%I64d", &p);
fds[i].pl.insert(p);
st.insert(p);
}
}
if(st.size() < m)cout << -1 << endl;
else solve();
}
return 0;
}
简要解释下:dp[p][j]表示解决前p题,需要数量最多的显示器的朋友是第i个,所需花费的最小代价。
于是,连续修修补补、添添改改交了N发,WA了N发。直到测试结束也没有A出来。
(2)然而,本题的真正原意是:对于每个朋友,如果他需要的显示器足够,那么,只要支付一次他需要的AC题目的费用,他就会把他会做的题目全部做掉。当然,AC了的题目再做一遍也是可以的。所以,本题应该要用状态压缩DP来做。
dp[s]表示当做了题的状态为s时,所需花费的最小代价。然后,不难想到递推式:dp[s | 第i个朋友会做的题号使用二进制法编码成的整数] = min(dp[s
| 第i个朋友会做的题号使用二进制法编码成的整数], dp[s] + xi)。咦?不是还要付显示器的钱吗?怎么搞?dp数组弄成结构体,成员为v和moni。dp[s].moni记录做了题的状态为s时,最多买了多少个显示器。
(3)还有一点就是,对状态的循环要放在对朋友的循环的里面,这个也是我没想明白的地方。暂时先搁着吧。再过段时间水平更高了,自然就明白了。
然后,就有了如下代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
typedef __int64 I64;
const I64 INF = ((I64)1) << 62;
I64 n, m, b;
typedef struct {
I64 x, k, m;
set<I64> pl;
I64 ac;
} Friend;
set<I64> st;
Friend fds[105];
typedef struct {
I64 v, moni;
} DP;
DP dp[1050000];
void init() {
st.clear();
for(I64 i = 0; i < 105; i++) {
fds[i].x = 0, fds[i].k = 0, fds[i].m = 0;
fds[i].pl.clear();
}
}
void solve() {
int ak = (1 << m) - 1;
I64 t;
for(int i = 0; i < (1 << 20); ++i) {
dp[i].v = INF;
dp[i].moni = 0;
}
dp[0].v = 0, dp[0].moni = 0;
for(int i = 1; i <= n; i++) {
for(int s = 0; s <= ak; ++s) {
if((s & fds[i].ac) == fds[i].ac)continue;
t = (fds[i].k > dp[s].moni ? fds[i].k - dp[s].moni : 0);
if(dp[s | fds[i].ac].v > dp[s].v + fds[i].x + t * b) {
dp[s | fds[i].ac].v = dp[s].v + fds[i].x + t * b;
dp[s | fds[i].ac].moni = dp[s].moni + t;
}
}
}
printf("%I64d\n", dp[ak].v);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Einput.txt", "r", stdin);
#endif // ONLINE_JUDGE
I64 p;
while(~scanf("%I64d%I64d%I64d", &n, &m, &b)) {
init();
for(I64 i = 1; i <= n; i++) {
scanf("%I64d%I64d%I64d", &fds[i].x, &fds[i].k, &fds[i].m);
for(I64 j = 0; j < fds[i].m; j++) {
scanf("%I64d", &p);
fds[i].pl.insert(p);
st.insert(p);
}
}
for(I64 i = 1; i <= n; ++i) {
I64 t = 0;
for(set<I64>::iterator it = fds[i].pl.begin(); it != fds[i].pl.end(); ++it)
t |= (((I64)1) << (*it - 1));
fds[i].ac = t;
}
if(st.size() < m)cout << -1 << endl;
else solve();
}
return 0;
}
然后,交上去在第24个测试样例挂了,结果如下所示。
Test: #24, time: 0 ms., memory: 18488 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER Input 59 11 830067068 735007990 107101946 6 1 2 4 5 6 10 603600908 880756312 8 1 2 4 7 8 9 10 11 659716960 174388278 5 2 5 7 9 10 899506226 611183471 8 2 4 5 7 8 9 10 11 571272365 68726728 6 4 5 7 8 10 11 330538629 560221018 6 3 4 5 8 10 11 686021635 919974878 6 1 4 5 6 8 9 2687375 998065071 7 1 3 4 5 8 9 11 141387697 606296659 5 2 3 4 7 9 287220643 466057605 6 2 4 6 7 8 11 822768448 695179109 4 2 3 4 10 696850261 919174199 7 1 4 6 7 8 9 10 71824861 156690160 6 1 3 6 7 10 11 875086545 ... Output 83528377136277204 Answer 83528376862632587 Checker Log wrong answer expected 83528376862632587, found 83528377136277204
对比一下可以发现,错误的结果前7个数字还是对的上的,就是后面的对不上了。错的我不明不白。想不明白。
然后,到网上看了别人的题解后,理解了下,普遍的做法是:dp还是简单数据类型数组,只是在dp到达目标状态时,使用min函数更新我们需要的答案,最大需要的显示器数量为当前朋友需要的显示器数量。还有就是,都说要对Friend结构体按需要的显示器个数k自然排序,我就想:为什么要排序呢?每种状态不是都能遍历到吗,为何还要排下序?因为,如果不排序,按照这种写法,就会出现这么一种情况:
2 1 b 1 3 1 1 2 2 1 1
对于该测试样例,得到这么一个结果,ans = 1 + 2 * b。即选第一个的费用x,然后由于题目已经刷完,达到了目标状态,对ans更新,ans = min(1 + 3 * b, 1 + 2 * b)。即用前面做题花的费用再加上当前朋友所需要的显示器*单个显示器价格,即:1 + 2 * b。显然,3 * b < 2 * b,所以,ans被更新为1 + 2 * b。然而,这种情况实际上是不合法的。所以,我们需要把这种情况干掉,就是通过排序。
(4)接下来,就是第4个坑了。排序的时候,因为是按照朋友所需要的显示器的数量从小大自然排序,于是我写了个这样的比较函数:
bool cmp(Friend f1, Friend f2) {
return f1.k <= f2.k;
}
样例一跑,发现都没错,果断交一发,嘣,我擦,RE。点开Codeforces提供的错误样例,发现输入数据全是最上界的值,然后我开始找数据越界的地方,找了半天还是没找到,最终一点一点注释,定位到了sort函数这里。按理来说,sort函数限定了上下界指针,在里面自由活动应该不会出问题的。然后我把cmp函数里面的“=”去掉,跑一下,竟然没事了。真坑。。。。。。这个问题,我也没想明白,为什么会这样。
于是最终的AC代码就是这样了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
typedef __int64 I64;
const I64 INF = 1LL << 62;
I64 n, m, b;
typedef struct {
I64 x, k, m, ac;
} Friend;
set<I64> st;
Friend fds[105];
I64 dp[1050000];
bool cmp(Friend f1, Friend f2) {
return f1.k < f2.k;
}
void solve() {
I64 ak = (I64)(1 << m) - 1;
I64 t, ans = INF;
sort(fds + 1, fds + n + 1, cmp);
for(int i = 0; i < (1 << 20); i++)dp[i] = INF;
dp[0] = 0;
for(I64 i = 1; i <= n; i++) {
for(I64 s = 0; s <= ak; ++s) {
if((s & fds[i].ac) == fds[i].ac)continue;
dp[s | fds[i].ac] = min(dp[s | fds[i].ac], dp[s] + fds[i].x);
}
if(dp[ak] != INF)ans = min(ans, dp[ak] + fds[i].k * b);
}
printf("%I64d\n", ans == INF ? -1 : ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Einput.txt", "r", stdin);
#endif // ONLINE_JUDGE
I64 p, t;
while(~scanf("%I64d%I64d%I64d", &n, &m, &b)) {
st.clear();
for(I64 i = 1; i <= n; i++) {
scanf("%I64d%I64d%I64d", &fds[i].x, &fds[i].k, &fds[i].m);
t = 0;
for(I64 j = 0; j < fds[i].m; j++) {
scanf("%I64d", &p);
st.insert(p);
t |= 1LL << (p - 1);
}
fds[i].ac = t;
}
if(st.size() < m)cout << -1 << endl;
else solve();
}
return 0;
}
(5)最后再做一次总结:
- 看题一定要仔细,否则做再多也是白搭。这个道理很多地方都是相通的,如果不理解人家,没搞明白人家的意思,做再多又有什么用?谁会买你的苦劳?别人只看结果的。
- 使用sort函数时,需要注意cmp函数的写法。一定不能再吃这样的哑亏了。
- 对于不明白的,不必急着刻意去想懂它,先搁一搁,平时走路、洗澡的时候可以想想,有了一段时间就能彻底明白了。现在还不懂就记住这些坑,下次写代码的时候注意好。明白了以后就自然不会再放错了。
Codeforces-417D总结&题解的更多相关文章
- Codeforces 417D Cunning Gena(状态压缩dp)
题目链接:Codeforces 417D Cunning Gena 题目大意:n个小伙伴.m道题目,每一个监视器b花费,给出n个小伙伴的佣金,所须要的监视器数,以及能够完毕的题目序号. 注意,这里仅仅 ...
- Codeforces Round #556 题解
Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...
- Cunning Gena CodeForces - 417D
Cunning Gena CodeForces - 417D 题意 先将小伙伴按需要的监视器数量排序.然后ans[i][j]表示前i个小伙伴完成j集合内题目所需最少钱.那么按顺序枚举小伙伴,用ans[ ...
- Codeforces Round #569 题解
Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...
- Codeforces Round #557 题解【更完了】
Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...
- CFEducational Codeforces Round 66题解报告
CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...
- codeforces CF475 ABC 题解
Bayan 2015 Contest Warm Up http://codeforces.com/contest/475 A - Bayan Bus B - Strongly Connected Ci ...
- Codeforces Round #542 题解
Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...
- 【codeforces 417D】Cunning Gena
[题目链接]:http://codeforces.com/problemset/problem/417/D [题意] 有n个人共同完成m个任务; 每个人有可以完成的任务集(不一定所有任务都能完成); ...
- Codeforces Choosing Laptop 题解
这题实在是太水了,具体看注释 蒟蒻的方法是一边找过时的电脑一边比大小 蒟蒻不才,只会C++ 其实还会free basic,但它已经过时了 附: 本题洛谷网址 Codeforces网址 希望蒟蒻的题解能 ...
随机推荐
- Conky配置文件
Conky是一个可以在linux系统中实时显示系统性能的工具,美观且十分好用,我们选择安装conky-all程序包 # set to yes if you want Conky to be forke ...
- New Concept English there (2)Typing speed exercise
typing speed (11words/ seconds) our vicar ia always rising money for one cause or another, but he ha ...
- EFM32 ARM+ KEIl program
1Hardware connection When using the EFM32 starter kit to make a JLINK burn, you must connect the con ...
- MVC4中视图获取控制器中返回的json格式数据
再开发MVC项目时,有时只需要从控制器中返回一个处理的结果,这时返回Json格式的数据非常的方便,在Controller中,提供了几种返回类型和方法,如: Content() 返回文本类型的Conte ...
- 有关项目依赖包发生 Manifest Merge 冲突的详细解决方案
安卓开发使用 Gradle 插件管理依赖包确实非常方便,尤其是在解决一些依赖冲突的问题上.比如,重复依赖的问题,具体内容请我之前写的一篇文章: 有关 Android Studio 重复引入包的问题和解 ...
- [置顶]
Android AOP 实践笔记
本文同步自wing的地方酒馆 最近博客更新越来越慢了,有两方面原因: 1.没啥好写的. 2.应该沉下心好好沉淀自己,积累一些东西,博客写的太频繁有"刷博客"之嫌,还容易浮躁. 浮躁 ...
- HDU 1374
http://acm.hdu.edu.cn/showproblem.php?pid=1374 已知三点坐标,求三点确定的圆的周长 #include <iostream> #include ...
- pytorch在CPU和GPU上加载模型
pytorch允许把在GPU上训练的模型加载到CPU上,也允许把在CPU上训练的模型加载到GPU上.CPU->CPU,GPU->GPU torch.load('gen_500000.pkl ...
- ibatis 参数类型为map,map里面有list
<select id="getChannelLayerList" parameterClass="java.util.HashMap" re ...
- linux vim 命令使用
基本上vim可以分为三种状态,分别是命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode) 模式切换方法 在命令模式输入“i”,进入插入模式 ...