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的更多相关文章

  1. BZOJ 3450: Tyvj1952 Easy [DP 概率]

    传送门 题意:$ox?$组成的序列,$?$等概率为$o\ or\ x$,得分为连续的$o$的长度的平方和,求期望得分 一开始没想出来,原因在于不知道如何记录长度 其实我们同时求得分和长度的期望就好了 ...

  2. 【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] = ...

  3. Code Forces 698A Vacations

    题目描述 Vasya has nn days of vacations! So he decided to improve his IT skills and do sport. Vasya know ...

  4. EOJ Monthly 2018.2

    A. 坑爹的售票机 题意 用\(1,5,10,25,50,100\)的纸币买\(n\)张单价为\(p\)的船票,且一次性最多买\(k\)张,求钱数恰好时最少需要多少张纸币. Hard: \(n,k,p ...

  5. EOJ Monthly 2018.1 F 最小OR路径

    题目链接 Description 给定一个有 \(n\) 个点和 \(m\) 条边的无向图,其中每一条边 \(e_i\) 都有一个权值记为 \(w_i\) . 对于给出的两个点 \(a\) 和 \(b ...

  6. ZOJ3802 Easy 2048 Again (状压DP)

    ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  7. 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 ...

  8. Codeforces 1077F1 Pictures with Kittens (easy version)(DP)

    题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...

  9. CF1096:D. Easy Problem(DP)

    Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement ...

随机推荐

  1. scala言语基础学习五

    extends override 和super方法 override field 父类不是val对象不能覆盖field isInstanceOf和asInstanceOf(isInstanceOf是用 ...

  2. google-http-java-client(android学习篇2源码)

    package com.google.api.services.samples.googleplus.cmdline.simple;   import com.google.api.client.ht ...

  3. 【P1326】超级教主

    DP优化 原题: LHX教主很能跳,因为Orz他的人太多了.教主跳需要消耗能量,每跳1米就会消耗1点能量,如果教主有很多能量就能跳很高.教主为了收集能量,来到了一个神秘的地方,这个地方凡人是进不来的. ...

  4. 【NOI2011】【P1308】道路修建

    这题也太水了吧,为什么不是我这届的NOI(╯‵□′)╯︵┻━┻ 原题: 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都 ...

  5. Codeforces Round #339 Div.2 A - Link/Cut Tree

    第一次正式参加常规赛想想有些小激动的呢 然后第一题就被hack了 心痛 _(:зゝ∠)_ tle点在于越界 因此结束循环条件从乘变为除 done //等等 这题没过总评 让我静静........ // ...

  6. 黑马程序员——JAVA基础之简述 类的继承、覆写

    ------- android培训.java培训.期待与您交流! ---------- 继承的概述: 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只 ...

  7. Java 控制反转和依赖注入模式【翻译】【整理】

    Inversion of Control Containers and the Dependency Injection pattern --Martin Fowler 本文内容 Component ...

  8. (转) Deep Learning in a Nutshell: Reinforcement Learning

    Deep Learning in a Nutshell: Reinforcement Learning   Share: Posted on September 8, 2016by Tim Dettm ...

  9. ES

    https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-service-win.html https://www.e ...

  10. gprof使用介绍

    gprof 1.1      简介 gprof实际上只是一个用于读取profile结果文件的工具.gprof采用混合方法来收集程序的统计信息,他使用检测方法,在编译过程中在函数入口处插入计数器用于收集 ...