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)\)一样,子序列只要 ...
随机推荐
- Python学习小计
1.初学Python最好选择2.7版本,因为大部分Python书籍的示例代码是基于这个版本的 2.Python安装可以参考百度经验完成 如果在电脑上同时安装2个版本,则CMD启动时只需要: py -2 ...
- [转]C# 位域[flags]
.NET中的枚举我们一般有两种用法,一是表示唯一的元素序列,例如一周里的各天:还有就是用来表示多种复合的状态.这个时候一般需要为枚举加上[Flags]特性标记为位域,例如: [Flags] enu ...
- redis的持久化功能
基于快照持久化 修改配置文件,开始基于快照的选项 [root@localhostbin]#vim /etc/redis/redis.conf stop-writes-on-bgsave-error y ...
- WIN7把任务栏的的蓝牙图标误删了找回方法
当时我删了以后,在网上找方法,都说—— 点击任务栏下面的三角箭头,选择自定义,里面有蓝牙图标选项,选择显示图标和通知. 可是我发现我的自定义选项里面就没有蓝牙图标选项啊... 故事的最后,我终于找到了 ...
- js数组及数组对象的遍历
一 数组遍历 方法一:for循环 方法二:forEach遍历 forEach遍历数组 性能低于for循环,且不可使用break中断循环,也不能使用return返回外层函数 arr.forEach(fu ...
- tp5页面跳转,空控制器空方法
namespace app\index\controller; use think\Controller; class Login extends Controller{ //显示html页面 pub ...
- ES6的基本语法
ES6 详细参考页面 简介 ECMAScript和JavaScript的关系是,前者是后者的规格,后者是前者的一种实现.一般来说,这两个词是可以互换的. let命令 ES6新增了let命令,用来声明变 ...
- c#将http调用返回额json中的有关中文的unicode转换为中文
c#将http调用返回额json中的有关中文的unicode转换为中文 中文转Unicode:HttpUtility.UrlEncodeUnicode(string str);转换后中文格式:&quo ...
- PHP小常识分享
PHP 标记 当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?php 和 ?>,这告诉 PHP 开始和停止解析二者之间的代码.此种解析方式使得 PHP 可以被嵌入到各种不同的文 ...
- Maven编译、打war包
Eclipse环境,每次alt+F5刷新Maven项目时,总是会把项目的Java依赖刷新成1.5. 解决办法:在pom中加入如下片段 <plugin> <groupId>org ...