easy dp
1.将一堆正整数分为2组,要求2组的和相差最小。
//File Name: nod1007.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年05月28日 星期六 20时12分23秒 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream> using namespace std; bool f[][];
int a[]; void solve(int n){
memset(f,false,sizeof f);
f[][] = true;
int now = ;
for(int i=;i<=n;i++){
now += a[i];
for(int j=;j<=now;j++){
f[i][j] = f[i-][j];
if(j >= a[i]) f[i][j] |= f[i-][j-a[i]];
}
}
int ans = ;
for(int i=;i<=now;i++){
if(!f[n][i]) continue;
ans = min(ans,abs(now - * i));
}
printf("%d\n",ans);
} int main(){
int n;
while(~scanf("%d",&n)){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
solve(n);
}
return ;
}
2.最长递增子序列
//File Name: nod1134.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年05月28日 星期六 20时45分45秒 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream> using namespace std; const int MAXN = + ;
const int INF = 0x3f3f3f3f; int d[MAXN];
int a[MAXN]; int bs(int l,int r,int x){
int mid;
while(r - l > ){
mid = (l + r) >> ;
if(d[mid] < x) l = mid;
else r = mid;
}
return l;
} int solve(int n){
int len = ;
d[] = -INF;
for(int i=,j;i<=n;i++){
if(a[i] > d[len]){
d[++len] = a[i];
}
j = bs(,len,a[i]);
d[++j] = a[i];
}
return len;
} int main(){
int n;
while(~scanf("%d",&n)){
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
printf("%d\n",solve(n));
}
return ;
}
3.最大子矩阵和
一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值
//File Name: nod1051.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年05月28日 星期六 21时22分03秒 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream> #define LL long long using namespace std; const int MAXN = ; LL b[MAXN],f[MAXN];
int a[MAXN][MAXN]; LL get(int n){
LL ans = ;
f[] = ;
for(int i=;i<=n;i++){
f[i] = max(f[i-],0LL) + b[i];
if(f[i] > ans) ans = f[i];
}
return ans;
} LL solve(int n,int m){
LL ans = -;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++) b[j] = ;
for(int j=i;j<=n;j++){
for(int k=;k<=m;k++)
b[k] = b[k] + a[j][k];
LL now = get(m);
if(now > ans) ans = now;
}
}
return ans;
} int main(){
int n,m;
while(~scanf("%d %d",&m,&n)){
bool flag = false;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&a[i][j]);
if(a[i][j] >= ) flag = true;
}
}
if(!flag) puts("");
else printf("%lld\n",solve(n,m));
}
return ;
}
easy dp的更多相关文章
- BZOJ 3450: Tyvj1952 Easy [DP 概率]
传送门 题意:$ox?$组成的序列,$?$等概率为$o\ or\ x$,得分为连续的$o$的长度的平方和,求期望得分 一开始没想出来,原因在于不知道如何记录长度 其实我们同时求得分和长度的期望就好了 ...
- 【dp入门题】【跟着14练dp吧...囧】
A HDU_2048 数塔 dp入门题——数塔问题:求路径的最大和: 状态方程: dp[i][j] = max(dp[i+1][j], dp[i+1][j+1])+a[i][j];dp[n][j] = ...
- Code Forces 698A Vacations
题目描述 Vasya has nn days of vacations! So he decided to improve his IT skills and do sport. Vasya know ...
- EOJ Monthly 2018.2
A. 坑爹的售票机 题意 用\(1,5,10,25,50,100\)的纸币买\(n\)张单价为\(p\)的船票,且一次性最多买\(k\)张,求钱数恰好时最少需要多少张纸币. Hard: \(n,k,p ...
- EOJ Monthly 2018.1 F 最小OR路径
题目链接 Description 给定一个有 \(n\) 个点和 \(m\) 条边的无向图,其中每一条边 \(e_i\) 都有一个权值记为 \(w_i\) . 对于给出的两个点 \(a\) 和 \(b ...
- ZOJ3802 Easy 2048 Again (状压DP)
ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- Easy 2048 Again - ZOJ 3802 像缩进dp
Easy 2048 Again Time Limit: 2 Seconds Memory Limit: 65536 KB Dark_sun knows that on a single-tr ...
- Codeforces 1077F1 Pictures with Kittens (easy version)(DP)
题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...
- CF1096:D. Easy Problem(DP)
Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement ...
随机推荐
- HDU 5234 Happy birthday --- 三维01背包
HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...
- UVa 1585 Score --- 水题
题目大意:给出一个由O和X组成的串(长度为1-80),统计得分. 每个O的分数为目前连续出现的O的个数,例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3 解题思路:用一个变量t ...
- Python爬虫学习笔记——豆瓣登陆(二)
昨天能够登陆成功,但是不能使用cookies,今天试了一下requests库的Session(),发现可以保持会话了,代码只是稍作改动. #-*- coding:utf-8 -*- import re ...
- netsh winsock reset
最近要在虚拟机上安装 git ,然后托管一个项目,搞得我是焦头烂额.今天下午我卸载了vmware workstation ,但是奇怪的事就发生了,我的google chrome 浏览器没法浏览网页了. ...
- 【转】GitHub 中国区前 100 名到底是什么样的人?
原文网址:http://mt.sohu.com/20160407/n443539407.shtml 本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百 ...
- CUDA Thread Indexing
1D grid of 1D blocks __device__ int getGlobalIdx_1D_1D() { return blockIdx.x *blockDim.x + threadIdx ...
- Unity3D深入浅出 - 新版粒子系统 (Shuriken) - Tonge
Shuriken粒子系统是继Unity3.5版本之后推出的新版粒子系统,它采用了模块化管理,个性化的粒子模块配合粒子曲线编辑器使用户更容易创作出各种兵分复杂的粒子效果. 创建一个粒子系统的方式有两种: ...
- Nginx反向代理讲解和配置
首先来介绍下Nginx的反向代理.代理服务器一般分为正向代理(通常直接称为代理服务器)和反向代理. 画个图我们就好理解了. 正向代理:可以想象成是路由器,我们要通过它来上网的那种.(可以说是客户端的代 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- oracle参数与启停
oracle随系统启动而启动 cs65-64桌面版orcle-11.2.0.4 启动监听器,后台进程,OEM. 注意: 如果只做一和三,只能启动后台进程,监听器不启动,如果只做二和三,只能启动监听器, ...