题目大意:

给定一堆点,具有x,y两个值

找到一组最多的序列,保证点由前到后,x严格上升,y严格下降,并把最大的数目和这一组根据点的编号输出来

这里用两种方法来求解:

1.

我们可以一开始就将数组根据x由大到小排个序,由前往后取,保证x严格上升了

只要每次取得过程中找一条x不相等的关于y的最长下降子序列即可,加个回溯,输出所有点

 #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = ;
int dp[N] , fa[N] , rec[N]; struct Node{
int x , y , id;
bool operator<(const Node &m)const{
if(x == m.x) return y < m.y;
return x < m.x;
}
}node[N]; int main()
{
//freopen("a.in" , "r" , stdin);
int k = ;
while(scanf("%d%d" , &node[k].x , &node[k].y) != EOF){
node[k].id = k + ;
k++;
}
//先保证都是以x由小到大排列
sort(node , node+k); memset(dp , , sizeof(dp));
memset(fa , - , sizeof(fa));
dp[] = ;
//x已满足上升,现在处理y使其按下降顺序排列即可,按照最长上升子序列的O(n^2)的方法类似处理即可
for(int i = ; i<k ; i++){
dp[i] = ;
for(int j = ; j<i ; j++){
if(node[j].x != node[i].x && node[i].y < node[j].y){
if(dp[i] < dp[j] + ){
dp[i] = dp[j] + ;
fa[i] = j;
}
}
}
} int maxn = , la;
for(int i = ; i<k ; i++){
if(maxn < dp[i]) maxn = dp[i] , la = i;
} int cnt = ;
rec[cnt++] = la;
while(fa[la] >= ){
rec[cnt++] = fa[la];
la = fa[la];
}
printf("%d\n" , maxn);
for(int i =cnt- ; i>= ; i--)
printf("%d\n" , node[rec[i]].id); return ;
}

2.

我们对所有m1.x<m2.x , m1.y>m2.y的m1和m2间添加一条有向线段

这样我们可以从所有入度为0的点出发,进行bfs,每一条边认为长度为1,入度为0的点认为dp[i] = 1表示到达第i个点最多能有多少个物品

计算最短路径的过程加个回溯这个题目就解决了

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
const int N = ;
int dp[N] , fa[N] , rec[N] , k , t , first[N];
int in[N] , vis[N];
queue<int> q; struct Node{
int x , y , id;
bool operator<(const Node &m)const{
if(x == m.x) return y < m.y;
return x < m.x;
}
}node[N]; struct Edge{
int y , next;
}e[N*N]; void add_edge(int x, int y)
{
e[k].y = y , e[k].next = first[x];
first[x] = k++;
} void bfs(int s)
{
memset(vis , , sizeof(vis));
q.push(s);
vis[s] = ;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = ;
for(int i = first[u] ; i!=- ; i = e[i].next){
int v = e[i].y;
if(dp[v] < dp[u] + ){
dp[v] = dp[u] + ;
fa[v] = u;
if(!vis[v]){
vis[v] = ;
q.push(v);
}
}
}
}
} bool ok(int ith1 , int ith2)
{
return node[ith1].x < node[ith2].x && node[ith1].y > node[ith2].y;
} int main()
{
// freopen("a.in" , "r" , stdin);
t = , k = ;
memset(first , - , sizeof(first));
memset(dp , , sizeof(dp));
memset(in , , sizeof(in));
while(scanf("%d%d" , &node[t].x , &node[t].y) != EOF){
node[t].id = t + ;
t++;
} for(int i = ; i<t ; i++){
for(int j = ; j<i ; j++){
if(ok(i , j)){
add_edge(i , j);
in[j] ++;
}
if(ok(j , i)){
add_edge(j , i);
in[i] ++;
}
}
} for(int i = ; i<t ; i++)
if(in[i] == ){
dp[i] = ;
bfs(i);
} int maxn = , la;
for(int i = ; i<t ; i++){
if(maxn < dp[i]){
maxn = max(maxn , dp[i]);
la = i;
}
} int cnt = ;
rec[cnt++] = la;
while(fa[la]){
rec[cnt++] = fa[la];
la = fa[la];
}
printf("%d\n" , maxn);
for(int i = cnt- ; i>= ; i--)
printf("%d\n" , rec[i]); return ;
}

HDU 1160 排序或者通过最短路两种方法解决的更多相关文章

  1. 两种方法解决 "The License CNEKJPQZEX- has been cancelled..." 问题

    今天在使用 2017 的 IDEA 和 Pycharm 等IDE的时候,提示了如题的问题.之前实在 http://idea.lanyus.com/ 网站点击生成注册码,复制粘贴到 IDEA 中就好了, ...

  2. 两种方法解决tomcat的 Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]

    出现这种原因主要是8080端口被占用了. 解决1: 打开任务管理器看看里面有没有javaw的线程,把它关了再重新启动tomcat看看. 解决2: 修改tomcat /conf /server.xml ...

  3. 解决vue项目eslint校验 Do not use 'new' for side effects 的两种方法

    import Vue from 'vue' import App from './App.vue' import router from './router' new Vue({ el: '#app' ...

  4. 转:python list排序的两种方法及实例讲解

    对List进行排序,Python提供了两个方法 方法1.用List的内建函数list.sort进行排序 list.sort(func=None, key=None, reverse=False) Py ...

  5. 用Java集合中的Collections.sort方法对list排序的两种方法

    用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  6. python list排序的两种方法及实例讲解

    对List进行排序,Python提供了两个方法方法1 用List的内建函数list sort进行排序list sort(func=None, key=None, reverse=False)Pytho ...

  7. MongoDB实现分页(两种方法)

    1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...

  8. 计算理论:NFA转DFA的两种方法

    本文将以两种方法实现NFA转DFA,并利用C语言实现. 方法二已利用HNU OJ系统验证,方法一迷之WA,但思路应该是对的,自试方案,测试均通过. (主要是思路,AC均浮云,大概又有什么奇怪的Case ...

  9. MySQL中删除数据的两种方法

    转自:http://blog.csdn.net/apache6/article/details/2778878 1. 在MySQL中有两种方法可以删除数据: 一种是delete语句,另一种是trunc ...

随机推荐

  1. 国王游戏 2012年NOIP全国联赛提高组(贪心+高精)

    P1080 国王游戏 题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成 ...

  2. ATX 学习 (三)-atxserver2-android-provider

    服务端代码 代码clone到本地,搭好相应环境(怎么搭的这里就不介绍了,很好搭的哈)一般库首先查看main.py文件,debug模式开始运行 一开始就是没接触过的tornado.ioloop,有点偏底 ...

  3. 牛客练习赛17-A-长方体

    题目描述 给出共享长方体一个顶点的三个面的面积,求它十二条边的边长和. 输入描述: 一行三个整数a, b, c表示面积(1 <= a, b, c <= 10000). 输出描述: 一行一个 ...

  4. 为WebSphere Application Server v8.5安装并配置JDK7

    IBM WebSphere Application Server v8.5可以同时支持不同版本的JDK共存,并且可以通过命令设置概要文件所使用的JDK版本.WAS8.5默认安装JDK6,如果要使用JD ...

  5. Linux命令(007) -- systemctl

    systemctl命令是系统服务管理指令,它实际上是将service和chkconfig两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd ...

  6. 376 Wiggle Subsequence 摆动序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  7. VC++常见错误原因解析--error LNK2019: 无法解析的外部符号 "public: void __thiscall

    根据个人遇到这个错误时的记录,原因可以分为一下几种: 原因一: 只是在.h里面声明了某个方法, 没有在cpp里面实现 . 具体讲,有时候在头文件中声明了需要的方法,确实忘记了在源文件中实现: 有时候在 ...

  8. 1682. [HAOI2014]贴海报

    1682. [HAOI2014]贴海报 ★★☆   输入文件:ha14d.in   输出文件:ha14d.out   简单对比 时间限制:1 s   内存限制:256 MB [题目描述] Byteto ...

  9. android视频播放器系列(二)——VideoView

    最近在学习视频相关的知识,现在也是在按部就班的一步步的来,如果有同样需求的同学可以跟着大家一起促进学习. 上一节说到了可以使用系统播放器以及浏览器播放本地以及网络视频,但是这在很大程度上并不能满足我们 ...

  10. Indy 编译提示版本不一致问题的解决

    1,起因 某delphi程序A使用了Indy9.0.18组件.机器中原本自带老版本的Indy组件9.0.12,后升级到9.0.18,使用一直正常. 某次操作将程序A重新build all了一下,结果提 ...