CodeForces 189A 166E 【DP ·水】
非常感谢 Potaty 大大的援助使得我最后A出了这两题DP
==================================
189A : 求切分后的ribbon最多的数目,不过要求切分后只能存在a or b or c 的长度
O(n)的效率:遍历下来求 f[i - a]、f[i - b]、 f[i - c] 中的最大值
如果i - a || b || c 的值小于0那么跳过
来一张图,过程非常清晰

当然,初始化对f 数组置-INF,否则可能出错
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ; int f[]; int main(){
int i, j, k, t, n, m, numCase = ;
int a, b, c;
while(cin >> n >> a >> b >> c){
for(i = ; i <= n; ++i){
f[i] = -INF;
}
f[] = ;
for(i = ; i <= n; ++i){
int tempa = i - a;
int tempb = i - b;
int tempc = i - c;
if(tempa >= ){
checkmax(f[i], + f[tempa]);
}
if(tempb >= ){
checkmax(f[i], + f[tempb]);
}
if(tempc >= ){
checkmax(f[i], + f[tempc]);
}
}
cout << f[n] << endl;
} return ;
}
======================================
166E: 这也是一道DP
起点在D,然后这是一个四面体
不难发现,其实A,B,C 是一样的,所以就不需要多开空间浪费了
需要开两个数组a[2] , b[2] 就够了
(通过 i & 1 来判断奇偶,还是头一次用TVT~)
这道题目通过过程模拟可以一下子发现规律:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ; int main(){
int i, j, k, t, n, m, numCase = ;
ll a[], b[];
while(EOF != scanf("%d",&n)){
memset(a, , sizeof(a));
memset(b, , sizeof(b));
a[] = ;
for(i = ; i <= n; ++i){
a[i & ] = ( * b[!(i & )]) % MOD;
b[i & ] = (( * b[!(i & )]) + a[!(i & )]) % MOD;
}
cout << a[n & ] << endl;
} return ;
}
CodeForces 189A 166E 【DP ·水】的更多相关文章
- Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
- ACM :漫漫上学路 -DP -水题
CSU 1772 漫漫上学路 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- [poj2247] Humble Numbers (DP水题)
DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...
- CodeForces.158A Next Round (水模拟)
CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...
- CodeForces 706C Hard problem (水DP)
题意:对于给定的n个字符串,可以花费a[i] 将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...
- Codeforces 474D Flowers dp(水
题目链接:点击打开链接 思路: 给定T k表示T组測试数据 每组case [l,r] 有2种物品a b.b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = d ...
- codeforces 637D D. Running with Obstacles(dp,水题,贪心)
题目链接: D. Running with Obstacles time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Codeforces 189A:Cut Ribbon(完全背包,DP)
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...
- dp水题 序列问题 (9道)
9道题.A了8道,A题看题解也没弄懂怎么维护m段子序列的,过一段时间再回来看看 dp试水 47:56:23 125:00:00 Overview Problem Status Rank ( ...
随机推荐
- Qt的目录依赖问题----怎样生成一个绿色的Qt软件包
Qt的目录依赖问题----怎样生成一个绿色的Qt软件包 一.核心问题: 如果将编译好的Qt文件拷贝到任何目录下,作为一个绿色软件库来使用? 二.原理讨论 由于Qt在编译的时候将安装路径硬编码到了库文件 ...
- MSSQL SERVER 2008 R2 无法连接到数据库,用户sa登录失败,错误:18456
原因:勾选了强制实施密码策略,但是设置的密码很简单依然可以,比如:123456 这是为什么?原来,这个功能要用到NetValidatePasswordPolicy() API这个函数. (该功能只有在 ...
- Json.Net系列教程 2.Net类型与JSON的映射关系
原文 Json.Net系列教程 2.Net类型与JSON的映射关系 首先谢谢大家的支持和关注.本章主要介绍.Net类型与JSON是如何映射的.我们知道JSON中类型基本上有三种:值类型,数组和对象.而 ...
- 在SQL2005中部署CLR 程序集
原文 在SQL2005中部署CLR 程序集 有关于CLR函数的用途和用法,请了解 SQL Server CLR 极速入门,启用.设计.部署.运行 http://www.yongfa365.com/It ...
- android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK
android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK ADB server didn ...
- 常用 API
运行 Java 程序的参数.使用 Scanner 获取键盘输入.使用 BufferedReader 获取键盘输入.System类.Runtime类.Object类.Java 7新增的 Objects ...
- Java中String、StringBuilder以及StringBuffer
原文出处: 海子 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问到的地方,今天就来和大家一起学习一下String.StringBuilder和StringBuffe ...
- Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互
Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互 Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互
- leetcode_question_70 Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Ext JS学习第九天 Ext基础之 扩展原生的javascript对象
此文来记录学习笔记: •Ext对于原生的javascript对象进行了一系列的扩展,我们把他们掌握好,更能深刻的体会Ext的架构,从而对我们的web开发更好的服务, 源码位置,我们可以从开发包的这个位 ...