HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化
HDU - 1160
给一些老鼠的体重和速度
要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减
并输出一种方案
原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且仅当 $a<c,b>d$ 然后找出最长链
...我们就按照他说的重新排个序,然后找LIS吧,不过还需要去路径还原
数据量可以用$O(n^2)$的算法
不过我这里用来$O(nlogn)$的算法加上一个路径还原
嗯,其实是在一个单调栈上乱搞的二分罢了....
最后要回溯一下并且记录答案才行

#include <bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pp pair<int,int>
#define rep(ii,a,b) for(int ii=a;ii<=b;ii++)
#define per(ii,a,b) for(int ii=a;ii>=b;ii--)
#define show(x) cout<<#x<<"="<<x<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define showa(b,a) cout<<#a<<"["<<b<<"]="<<a[b]<<endl;
using namespace std;
const int maxn=1e5+10;
const int maxm=1e6+10;
const int INF=0x3f3f3f3f;
int casn,n,m,k;
struct node {
int a,b,id;
int operator <(node x){
if(x.a==a) return b>x.b;
else return a<x.a;
}
}ms[maxn];
int pre[maxn];
int stk[maxn],top;
int ans[maxn];
int main(){
//#define test
#ifdef test
freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif while(cin>>ms[n].a>>ms[n].b){ms[n].id=n+1;n++;}
sort(ms,ms+n);
memset(stk,0xc0,sizeof stk);
rep(i,0,n-1) ms[i].b=-ms[i].b;
rep(i,0,n-1){
int b=ms[i].b;
if(stk[top]<b){
stk[++top]=b;
pre[i]=top;
}else {
int l=1,r=top;
while(l<r){
int mid=(l+r)>>1;
if(stk[mid]<b)l=mid+1;
else r=mid;
}
stk[l]=b;
pre[i]=l;
}
}
cout<<top<<endl;
int t=top;
per(i,n,0){
if(pre[i]==t) ans[--t]=ms[i].id;
if(t<0) break;
}
rep(i,0,top-1) cout<<ans[i]<<endl;
#ifdef test
fclose(stdin);fclose(stdout);system("gedit ./out.txt");
#endif
return 0;
}
HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化的更多相关文章
- HDU 1160 FatMouse's Speed 动态规划 记录路径的最长上升子序列变形
题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了 ...
- 题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- HDU 1160 FatMouse's Speed(要记录路径的二维LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1160 FatMouse's Speed (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- HDU 1160 FatMouse's Speed LIS DP
http://acm.hdu.edu.cn/showproblem.php?pid=1160 同样是先按它的体重由小到大排,相同就按speed排就行. 这样做的好处是,能用O(n^2)枚举,因为前面的 ...
- hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- hdu 1160 FatMouse's Speed(最长不下降子序列+输出路径)
题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to ...
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...
随机推荐
- python django基础五 ORM多表操作
首先在创建表的时候看下分析一下 1.作者表和作者详细地址表 一对一关系 理论上谁都能当主表 把Author设置成主表 au=models.OneToOneField(to='AuthorDetail ...
- UVALive - 7041 The Problem to Slow Down You (回文树)
https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...
- 2016vijos 6-1 松鼠聚会(LCA+卡空间)
求LCA,N=1e6,原空间限制8MB 求LCA需要深度,需要跳跃一定距离的祖先,需要父节点 把一个整数压成3个char,f[]存父节点 g[],深度为奇数的点存往上跳576步能到的点,深度为偶数的点 ...
- Map接口、HashMap类、LinkedHashSet类
java.util.Map<K, V>接口 双列集合,key不可以重复 Map方法: 1.public V put(K key, V value):键值对添加到map,如果key不重复返回 ...
- jqgrid again
之前有提及过jqgrid这个很不错的jquery grid, 非常适合企业MIS系统使用. 本文以一个显示学生成绩结果的页面, 来说明它的一些用法, 手写代码, 运行可能会报错, 不过思路是经过验证的 ...
- 27. Spring Boot 缓存注解详解: @Cacheable、@CachePut、 @CacheEvict、@Caching、@CacheConfig
1.使用OGNL的命名规则来定义Key的值 @Cacheable(cacheNames = {"user"},key = "#root.methodName + '[' ...
- RMQ st算法 区间最值模板
#include<bits/stdc++.h> ; ; int f[N][Logn],a[N],lg[N],n,m; int main(){ cin>>n>>m; ...
- C# MVC EF框架 用事务
using System.Transactions; [HttpPost] public JsonResult Update(InfoModel list) { using (TransactionS ...
- 关于PHP建立数据库访问类的封装以及操作php单例模式连接数据库封装类
建立数据库访问类的封装 <?php class DBDA { public $host = "localhost"; //服务器地址 public $ui ...
- Expression 生成 Lambda
public static event Func<Student, bool> myevent; public delegate void del(int i, int j); stati ...