http://codeforces.com/contest/792/problem/C

这题奇葩题我居然用dp过了。

如果要模拟的话,可以用一个栈保存,保存每一个%3 = 2的pos,%3 = 1的pos,注意到题目是最多删除2个数,就能使得整个数%3=0了,如果要删除前导0的话就另外算。

那么贪心从栈顶删除,也就是先删除后面的数就行。然后需要删除2,又分两种情况,删除两个1和删除一个2。。等等,一路模拟。

比赛的时候没想到这样,也觉得很复杂,于是就dp了,虽然TLE了,但是加了一个剪枝就过了,dfs很玄

我用dp[i][j]表示,前i位中,模3后余数是j的最大合法长度,最大合法长度,也就是不能含有前导0.所以1001这样的情况求出来是无解的,需要特判一下。

那么求到了这个长度之后

题目就变成了,给出n个数字,选出k个,使得组合起来得数字%3 = 0,不能含有前导0.

我想不到好的算法,就dfs暴力了。感觉应该会超时,但是剪了剪支居然46ms

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
char str[maxn];
int dp[maxn][], lenstr; //dp出答案
vector<char>ans;
bool dfs(int cur, int now, int len, int pre) {
if (now == && len == dp[lenstr][]) return true;
if (cur == lenstr + ) return false;
if (len >= dp[lenstr][]) return false;
if (lenstr - cur + + len < dp[lenstr][]) return false;
if (str[cur] == '') {
if (pre) {
if (dfs(cur + , now, len + , )) {
ans.push_back(str[cur]);
return true;
}
return dfs(cur + , now, len, pre);
} else {
return dfs(cur + , now, len, pre);
}
} else {
if (dfs(cur + , (now + str[cur] - '') % , len + , )) {
ans.push_back(str[cur]);
return true;
}
return dfs(cur + , now, len, pre);
}
}
void work() {
scanf("%s", str + );
lenstr = strlen(str + );
memset(dp, -0x3f, sizeof dp);
dp[][] = ;
int flag = inf;
for (int i = ; i <= lenstr; ++i) {
if ((str[i] - '') % == ) flag = i;
for (int j = ; j < && i > ; ++j) {
dp[i][j] = dp[i - ][j];
}
//不是0的,自己作为一个
if (str[i] != '') dp[i][(str[i] - '') % ] = max(dp[i][(str[i] - '') % ], );
for (int j = ; j < ; ++j) {
int res = (j * + str[i] - '') % ;
dp[i][res] = max(dp[i][res], dp[i - ][j] + );
}
}
// cout << dp[lenstr][0] << endl;
if (dp[lenstr][] < && flag == inf) {
cout << - << endl;
return;
}
if (dp[lenstr][] < && flag != inf) {
cout << str[flag];
return;
}
dfs(, , , );
reverse(ans.begin(), ans.end());
for (int i = ; i < ans.size(); ++i) {
cout << ans[i];
}
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

C. Divide by Three DP的更多相关文章

  1. HDU 4301 Divide Chocolate (DP + 递推)

    Divide Chocolate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. CodeForces - 792C Divide by Three (DP做法)

    C. Divide by Three time limit per test: 1 second memory limit per test: 256 megabytes input: standar ...

  3. Educational Codeforces Round 18 C. Divide by Three DP

    C. Divide by Three   A positive integer number n is written on a blackboard. It consists of not more ...

  4. HDU 4301 Divide Chocolate(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4301 题意: 有一块n*2大小的巧克力,现在某人要将这巧克力分成k个部分,每个部分大小随意,问有多少种分法. 思 ...

  5. XVII Open Cup named after E.V. Pankratiev. GP of SPb

    A. Array Factory 将下标按前缀和排序,然后双指针,维护最大的右边界即可. #include<cstdio> #include<algorithm> using ...

  6. UOJ276 [清华集训2016] 汽水 【二分答案】【点分治】【树状数组】

    题目分析: 这种乱七八糟的题目一看就是点分治,答案有单调性,所以还可以二分答案. 我们每次二分的时候考虑答案会不会大于等于某个值,注意到系数$k$是无意义的,因为我们可以通过转化使得$k=0$. 合并 ...

  7. LOJ6502. 「雅礼集训 2018 Day4」Divide(构造+dp)

    题目链接 https://loj.ac/problem/6502 题解 中间一档部分分提示我们将所有的 \(w_i\) 排序. 考虑如果我们能构造出这样一个 \(w_i\) 的序列,使得该序列满足:对 ...

  8. 【概率dp】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs

    直接暴力dp就行……f(i,j)表示前i天集齐j种类的可能性.不超过10000天就能满足要求. #include<cstdio> using namespace std; #define ...

  9. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

随机推荐

  1. oracle查看锁表进程,杀掉锁表进程

    查看锁表进程SQL语句1: select sess.sid,     sess.serial#,     lo.oracle_username,     lo.os_user_name,     ao ...

  2. 程序员代码面试指南:IT名企算法与数据结构题目最优解

      第1章栈和队列 1设计一个有getMin功能的栈(士★☆☆☆) 1由两个栈组成的队列(尉★★☆☆) 5如何仅用递归函数和栈操作逆序一个栈(尉★★☆☆) 8猫狗队列(士★☆☆☆)10用一个栈实现另一 ...

  3. SPOJ:Red John is Back(DP)

    Red John has committed another murder. But this time, he doesn't leave a red smiley behind. What he ...

  4. shell之sort和uniq 及wc 的使用

    文本排序:sort       -n:数值排序       -r: 降序       -t: 字段分隔符       -k: 以哪个字段为关键字进行排序       -u: 排序后相同的行只显示一次 ...

  5. 阻止Eclipse一直building workspace

    Eclipse 一直不停 building workspace完美解决总结 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭 3.maven下载lib挂起 等.. 二.解决总结 (1).解决方 ...

  6. Codeforces Round #535(div 3) 简要题解

    Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...

  7. 洛谷P3385判负环——spfa

    题目:https://www.luogu.org/problemnew/show/P3385 两种方法,dfs和bfs: 一开始写的dfs,要把dis数组初值赋成0,这样从一个连着负边的点开始搜: 在 ...

  8. 二、mysql安装详解

    step1:打开下载的mysql安装文件(mysql-5.5.36-win32.msi),双击运行,如下图: step2:点击“Next”按钮继续,如下图: step3:点击“勾选”,点击“Next” ...

  9. div显示2列

    #wdjContainer{ border:1px solid green; margin:10px auto; width:500px; height:300px; line-height:30px ...

  10. Fitnesse中TemplateLibrary的使用方法

    1.新建一个SuitePage,命名为TemplateLibrary 2.然后如下图,添加作为template的TestPage,如下面的Get 3.在Get page中添加template内容,如下 ...