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)\)一样,子序列只要 ...
随机推荐
- Codeforces 994B. Knights of a Polygonal Table
解题思路 将骑士按力量从小到大排序,到第i个骑士的时候,前面的i-1个骑士他都可以击败,找出金币最多的k个. 用multiset存金币最多的k个骑士的金币数,如果多余k个,则删除金币数最小的,直到只有 ...
- Android7.0打开sdacrd图片问题
1.点击item,通过intent打开指定路径的图片. 2.测试6.0,5.0正常运行代码: File file=new File(item.address): Intent intent = new ...
- 转:Hibernate中Criteria和DetachedCriteria的完整用法
原文地址:http://blog.sina.com.cn/s/blog_667528fd0100rkrf.html 设计上可以灵活的根据 Criteria 的特点来方便地进行查询条件的组装.现在对 H ...
- 第七章 Python之模块与包
模块介绍 一个模块就是包含了一组功能的python文件(例如module.py,模块名是module),它从文件级别组织程序,更方便管理,这时我们不仅仅可以把这些文件当作脚本执行,还可以把他们当作模块 ...
- day05-2 变量、常量、注释以及内存管理
目录 什么是变量 Python中定义变量 定义变量名的命名规范 什么是常量 定义常量 注释是什么 注释有什么用 内存管理(重要) 引用计数 垃圾回收机制 小整数池 定义变量的三个特征 什么是变量 变量 ...
- anshi
env.js主要是帮助我们读取根目录下以.env开头的环境变量,并判断是否生效. 比如在根目录下创建一个.env.local文件 改写一个环境变量 而没有改之前,它是在3000端口打开 path.js ...
- Git的选项参数
git的选项参数 MisSa@DESKTOP-PIQ06QO MINGW64 ~ $ git usage: git [--version] [--help] [-C <path>] [-c ...
- Windows下安装XAMPP,Wordpress
配置XAMPP: 1.下载:https://www.apachefriends.org/zh_cn/download.html(下载速度日了狗!) 2.安装XAMPP; 3.启动apache, ...
- redis 篇 - list
list 类似于 Python list lpush key value 向列表append value lrange key start stop 获取下标从 start 到 stop 的value ...
- 安装node-sass及报错后解决方案
使用npm install 命令安装node-sass时,经常出现安装失败的情况.原因在于npm服务器在美国,还有就是某强大的防火墙作用.导致模块无法下载. 1 npm install node-sa ...