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 ...
随机推荐
- 团队项目开发中,常见的版本控制有svn,git
团队项目开发中,常见的版本控制有svn,git
- 【NOIP2010】引水入城
以前一直以为是什么高端DP,看了题解才发现是水题,老是这样看题解才能写出来到赛场上怎么办嘛QAQ 原题: 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好 ...
- Python 基础语法(四)
Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...
- js动态生成数据列表
我们通常会使用table标签来展示数据内容,由于需要展示的数据内容是随时更换的,所以不可能将展示的数据列表写死在html写死在页面中,而是需要我们根据后台传来的数据随时更换,这个时候就需要我们使用js ...
- ruby杂记
ruby基本类中的方法:puts Object.private_instance_methods
- SQL Server 索引分类
什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...
- 002. C#生成GUID
什么是GUID: 全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.该算法使用了太网卡地址, 纳秒级时间,CPU芯片I ...
- shell脚本实例-游戏脚本
http://bbs.chinaunix.net/thread-3580033-1-1.html shell游戏收集贴 #!/bin/bash # Tetris Game #APP declarati ...
- 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)
// The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...
- morris.js 简单学习
需要添加的引用脚本 <script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js&qu ...