http://codeforces.com/contest/6/problem/D

题目大意:有一队人,排成一列,每个人都有生命值,你每次可以攻击2~n位置的一个的人,假设每次攻击的位置为pos,那么pos位受到a点伤害,pos-1和pos+1受到b点伤害。问让所有人生命值都小于0所需要的最少操作数。

思路:最近在加强dp类型的题目,但是因为做过一点IDA*,感觉这题貌似可以用IDA*差不多的思路来做哈?(关键是我不会DP,TAT)然后我们找寻一下IDA*的可行性剪枝就好了:目前数列的maxval<=(lim - deep) * a即可,否则TLE了

这是IDA*的代码

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = ;
const int inf = 0x3f3f3f3f;
int road[maxn * maxn];
int n, a, b;
int h[maxn]; bool dfs(int pos, int deep, int lim){ int maxcnt = -inf;
for (int i = ; i <= n; i++) maxcnt = max(maxcnt, h[i]);
if (deep > lim) return false;
if (maxcnt > (lim - deep) * a) return false;//加上这个剪枝就变得超快
if (deep == lim){
if (maxcnt >= )return false;
else return true;
}
if (pos == n) return false; int cnt = max(h[pos - ] / b, max(h[pos] / a, h[pos + ] / b)) + ;
for (int cnt1 = ; cnt1 <= cnt; cnt1++){
if (h[pos - ] >= cnt1 * b) continue;
h[pos - ] -= b * cnt1; h[pos] -= a * cnt1; h[pos + ] -= b * cnt1;
for (int j = deep + ; j <= deep + cnt1; j++)
road[j] = pos;
if (dfs(pos + , deep + cnt1, lim)) {
for (int j = deep + ; j <= deep + cnt1; j++) road[j] = pos;
return true;
}
h[pos - ] += b * cnt1; h[pos] += a * cnt1; h[pos + ] += b * cnt1;
}
return false;
} int main(){
memset(road, , sizeof(road));
cin >> n >> a >> b;
int beg = ;
for (int i = ; i <= n; i++) scanf("%d", h + i);
for (int i = ; i <= * ; i++)
if (dfs(, , i)){
printf("%d\n", i);
for (int j = ; j <= i; j++)
printf("%d%c", road[j], j == i ? '\n' : ' ');
break;
}
return ;
}

明天附上DP代码。晚安~

dp思路:

定义dp[i][j][k][z]表示目前攻击第i个人,j表示第i-1个人的生命,k表示第i个人的生命,z表示第i+1个人的生命。然后转移需要注意一下以下方面

①简单的转移:dp[i][j - b][k - a][z - b] = dp[i][j][k][z];

②如果j-b以后的生命值为0了,那么dp[i+1][k - a][z - b][h[i + 2]]=dp[i][j][k][z] + 1;

③如果j本来就为0,那么dp[i+1][k][z][h[i + 2]] = dp[i][j][k][z];

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = ;
const int inf = 0x3f3f3f3f;
struct Point{
int i, j, k, z;
Point(int i = , int j = , int k = , int z = ): i(i), j(j), k(k), z(z){}
}par[maxn][maxn][maxn][maxn];
int dp[maxn][maxn][maxn][maxn];
int n, a, b;
int h[maxn]; int main(){
cin >> n >> a >> b;
for (int i = ; i <= n; i++){
scanf("%d", h + i); h[i]++;
}
memset(par, -, sizeof(par));
memset(dp, 0x3f, sizeof(dp));
dp[][h[]][h[]][h[]] = ;
for (int i = ; i <= n - ; i++){
for (int j = h[i - ]; j >= ; j--){
for (int k = h[i]; k >= ; k--){
for (int z = h[i + ]; z >= ; z--){
int nj = max(, j - b), nk = max(, k - a), nz = max(, z - b);
if (dp[i][nj][nk][nz] > dp[i][j][k][z] + ){
dp[i][nj][nk][nz] = dp[i][j][k][z] + ;
par[i][nj][nk][nz] = Point(i, j, k, z);
}
if (nj == && dp[i + ][nk][nz][h[i + ]] > dp[i][j][k][z] + ){
dp[i + ][nk][nz][h[i + ]] = dp[i][j][k][z] + ;
par[i + ][nk][nz][h[i + ]] = Point(i, j, k, z);
}
if (j == && dp[i + ][k][z][h[i + ]] > dp[i][j][k][z]){
dp[i + ][k][z][h[i + ]] = dp[i][j][k][z];
par[i + ][k][z][h[i + ]] = par[i][j][k][z];
}
}
}
}
}
int ans = dp[n][][][];
printf("%d\n", ans);
Point tmp = Point(n, , , );
while (ans--){
tmp = par[tmp.i][tmp.j][tmp.k][tmp.z];
printf("%d ", tmp.i);
}
return ;
}

四维dp 或者 剪枝 + dfs Codeforces Beta Round #6 (Div. 2 Only) D的更多相关文章

  1. 暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

    题目传送门 /* 题意:求最大矩形(全0)的面积 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 详细解释:http://www ...

  2. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  3. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  4. Codeforces Beta Round #65 (Div. 2)

    Codeforces Beta Round #65 (Div. 2) http://codeforces.com/contest/71 A #include<bits/stdc++.h> ...

  5. Codeforces Beta Round #59 (Div. 2)

    Codeforces Beta Round #59 (Div. 2) http://codeforces.com/contest/63 A #include<bits/stdc++.h> ...

  6. Codeforces Beta Round #52 (Div. 2)

    Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...

  7. Codeforces Beta Round #40 (Div. 2)

    Codeforces Beta Round #40 (Div. 2) http://codeforces.com/contest/41 A #include<bits/stdc++.h> ...

  8. Codeforces Beta Round #22 (Div. 2 Only)

    Codeforces Beta Round #22 (Div. 2 Only) http://codeforces.com/contest/22 A 水题 #include<bits/stdc+ ...

  9. Codeforces Beta Round #14 (Div. 2)

    Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...

随机推荐

  1. 数据库出现1045 access denied for user 'root'@'localhost' using password yes (转)

    在mysql命令行中执行 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');  GRANT ALL PRIVILEGES ON *.*  ...

  2. 第六十五,html嵌入元素

    html嵌入元素 学习要点:     1.嵌入元素总汇     2.嵌入元素解析     本章主要探讨HTML5中嵌入元素,嵌入元素主要功能是把外部的一些资源插入到HTML中.   一.嵌入元素总汇 ...

  3. ajax格式,需要指定交互的data类型

    思路 先打印所有返回值 console.log(d);  {''status'':999} 再查看返回值,能否转换为json dataType:'json',    //大小写敏感. datatype ...

  4. NSDictionary 的内部实现

    NSDictionary是IOS中使用的一种key-value容器,参考cocotron的源代码,NSDictionary使用NSMapTable实现. NSMapTable同样是一个key-valu ...

  5. MT4 图表上设置字符

    ; int start() { //---- ObjectCreate(, , ); string str = "<"; string show ; ;i<x;i++) ...

  6. OC之消息调用过程

    Bird * aBird = [[Bird alloc] init]; [aBird fly]; 中对 fly 的调用,编译器通过插入一些代码,将之转换为对方法具体实现 IMP 的调用,这个 IMP ...

  7. iOS应用性能调优的4个建议和技巧

    任何一个能在用户手机屏幕中占有一席之地的iOS app都包含3个关键因素:想法好.设计出色.性能卓越.本文将分享一些iOS应用性能调优的4个建议和技巧. Tip #1:把图片资源压缩到最小.    i ...

  8. (╭ ̄3 ̄)╭ 小希的迷宫II

    (╭ ̄3 ̄)╭ 小希的迷宫II TimeLimit: 2000/1000 MS (Java/Others)  MenoryLimit: 65536/32768 K (Java/Others) 64-b ...

  9. 什么是JDBC?

    JDBC是Java数据库连接(Java DataBase Connectivity)技术的简称,提供连接各种常用数据库的能力! 1.方式一(配置文件实现): <!-- 1. 连接池实例 --&g ...

  10. split分割字符串时的一些特殊分隔符

    @Test public void testSplit() { String address = "上海.上海市.闵行区.吴中路"; String[] splitAddress = ...