hdu 1257/1800 - 贪心,dp
1257题目链接
一个序列划分子序列,每个子序列都是非增序列,问最少分成几个子序列
1800题目链接
一堆数分组,每组内数据严格递减,问最少分几组
------------------------------------------------------------------------------------------------
这两个题的区别在于1257数组的排列顺序不能变,而1800只是分组,顺序随意。
1257有两种解法:
一是像hdu1051那样的贪心:像筛素数那样一个序列一个序列的标记。
一是求最长严格递增序列的长度,动态规划做。
而1800只需要统计每个数字出现的次数,找到最大的出现次数,注意前导0
1257贪心代码,0ms
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
typedef long long LL;
using namespace std;
const int N = ;
int hs[N];
bool vis[N];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++) scanf("%d",hs+i);
memset(vis,false,sizeof(vis));
int ans = ;
for(int i=;i<n;i++){
if(!vis[i]){
ans++;
int last = hs[i];
for(int j=i+;j<n;j++){
if((!vis[j])&&hs[j]<=last) last=hs[j],vis[j]=true;
}
}
}
printf("%d\n",ans);
}
return ;
}
1257dp代码,31ms
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
typedef long long LL;
using namespace std;
const int N = ;
int hs[N],dp[N];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++) scanf("%d",hs+i);
int ans = ;
for(int i=;i<n;i++){
dp[i]=;
for(int j=;j<i;j++){
if(hs[j]<hs[i]) dp[i]=MAX(dp[i],dp[j]+);
}
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
}
return ;
}
1800,用map统计
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
typedef long long LL;
using namespace std;
const int N = ; int main(){
int n,id;
char tmp[];
map<string,int> strs;
while(scanf("%d",&n)!=EOF){
if(!n){puts("");continue;} for(int i=;i<n;i++){
scanf("%s",tmp);
for(id=;id<strlen(tmp);id++){ if(tmp[id]!='') break; }
if(id==strlen(tmp)) id--; if(strs.count(tmp+id)) strs[tmp+id]++;
else strs[tmp+id]=;
}
int ans = ;
for(map<string,int>::iterator iter=strs.begin();iter!=strs.end();iter++)
ans = MAX(ans,iter->second);
printf("%d\n",ans);
strs.clear();
}
return ;
}
hdu 1257/1800 - 贪心,dp的更多相关文章
- hdu 5037 Frog 贪心 dp
哎,注意细节啊,,,,,,,思维的严密性..... 11699193 2014-09-22 08:46:42 Accepted 5037 796MS 1864K 2204 B G++ czy Frog ...
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1257 最少拦截系统 (DP || 贪心)
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 【贪心】HDU 1257
HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
- 怒刷DP之 HDU 1257
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- HDU 1257 最少拦截系统(Dilworth定理+LIS)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU 1257——最少拦截系统——————【LIS变型题】
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- HDU 1257 最少拦截系统 最长递增子序列
HDU 1257 最少拦截系统 最长递增子序列 题意 这个题的意思是说给你\(n\)个数,让你找到他最长的并且递增的子序列\((LIS)\).这里和最长公共子序列一样\((LCS)\)一样,子序列只要 ...
随机推荐
- Mediator 基于内存的发布订阅
Github Mediator 使用方法 /// <summary> /// 返回值 BaseEntity /// </summary> public class Ping1 ...
- windows下安装reidis
下载windows下redis安装包 https://github.com/MSOpenTech/redis/releases 这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了. 切换 ...
- WebApi在MVC 4中一个Controll多个post方法报错处理
http://blog.csdn.net/lqh4188/article/details/53542400(原创)
- GreenDao 3.X之基本使用
在GreenDao 3.X之注解已经了解到GreenDao 3.0的改动及注解.对于数据库的操作,无异于增删改查等四个操作.下面我们将了解GreenDao 3.X如何使用? AbstractDao 所 ...
- 伍、ajax
一.ajax的概念 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新. 传统的网页(不使用 AJAX)如果需要更新 ...
- BZOJ 2820: YY的GCD 莫比乌斯反演_数学推导_线性筛
Code: #include <cstdio> #include <algorithm> #include <cstring> #include <vecto ...
- 给iview组件select设置默认值
1.首先,给select加一个v-model,如: <Select v-model="exam_name" > <Option v-for="(item ...
- HDU 5762 Teacher Bo ( 暴力 )
链接:传送门 题意:给出N个点( Xi , Yi ),和点的最远位置M,询问是否有这样的四个点 (A,B,C,D)(A<B,C<D,A≠CorB≠D) ,AB的曼哈顿路径长度等于CD的曼哈 ...
- ansible 连通测试
[root@ftp:/root] > ansible ansible01 -m ping ansible01 | UNREACHABLE! => { "changed" ...
- Linux系统串口接收数据编
http://blog.csdn.net/bg2bkk/article/details/8668576 之前基于IBM deveplopworks社区的代码,做了串口初始化和发送的程序,今天在此基础上 ...