当前位置: 首页 > news >正文

Codeforces Beta Round 4 (Div. 2 Only) 4D. Mysterious Present (最长上升子序列变形)

题目:

Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1,  a2,  ...,  an}, where the width and the height of the i-th envelope is strictly higher than the width and the height of the (i  -  1)-th envelope respectively. Chain size is the number of envelopes in the chain.

Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he'll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It's forbidden to turn the card and the envelopes.

Peter has very many envelopes and very little time, this hard task is entrusted to you.

Input

The first line contains integers nwh (1  ≤ n ≤ 5000, 1 ≤ w,  h  ≤ 106) — amount of envelopes Peter has, the card width and height respectively. Then there follow n lines, each of them contains two integer numbers wi and hi — width and height of the i-th envelope (1 ≤ wi,  hi ≤ 106).

Output

In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers.

If the card does not fit into any of the envelopes, print number 0 in the single line.

样例:

Input:

2 1 1
2 2
2 2

Output:

1
1 

Input:

3 3 3
5 4
12 11
9 8

Output:

3
1 3 2 

单词:(我是废物)

// envelope n. 信封
// chain n.链
// respectively adv. 各自,分别,依次为

思路:

这道题和 AcWing 1012. 友好城市 思路基本一致,建议先写一下AcWing的那道题,会ez一点。

简单来说,就是对于w,h分别为第一第二关键字排序,在保证w已经递增(非严格递增)的情况下,对h求最长上升子序列,同时记录一下pre值,方便后续回溯路径。

但是这道题的坑很多,首先就是必须保证严格单调递增和排序,这就需要去重,这里我用了一个hash表去写。

以及最后如果chain的size 等于0,直接输出一个0就行了,不需要再输出编号。(调了一个小时T_T )

参考代码如下:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<unordered_map>using namespace std;const int N = 5010;int n, m, w, h;
struct env
{int w, h;int id;bool operator< (const env& t){return w != t.w ? w < t.w : h < t.h;}
}a[N];
unordered_map<long long, bool> st;int f[N], ans[N], pre[N];int main()
{cin >> n;for (int i = 0; i <= n; i++) {cin >> w >> h;if (w > a[0].w && h > a[0].h && !st[w * 1000000 + h]){a[m++] = { w, h, i };st[w * 1000000 + h] = 1;}}m--;sort(a + 1, a + m + 1);for (int i = 1; i <= m; i++){f[i] = 1;for (int j = 1; j < i; j++)if (a[i].h > a[j].h && a[i].w != a[j].w){f[i] = max(f[i], f[j] + 1);if (f[i] == f[j] + 1) pre[i] = j;}}int res = 0, p = 0;for (int i = 1; i <= m; i++){if (res < f[i]){res = f[i];p = i;}}cout << res << endl;int x = p, cnt = 0;while (pre[x] != 0){ans[cnt++] = x;x = pre[x];}ans[cnt++] = x;reverse(ans, ans + cnt);if (ans[0] == 0 && cnt == 1) return 0;for (int i = 0; i < cnt; i++) cout << a[ans[i]].id << ' ';return 0;
}


http://www.mrgr.cn/news/35755.html

相关文章:

  • Ubuntu环境切换到服务器某个用户后source等命令和Tab快捷补全都用不了了,提示没找到,但root用户可以
  • PCDN技术如何适应不同用户的需求和网络环境的变化?(贰)
  • OpenAI发布多语言MMMLU数据集;火山引擎发布AI视频生成大模型豆包
  • 大量数据分批次处理+concurrent.futures.ThreadPoolExecutor多线程处理文件
  • 【LeetCode每日一题】——LCP 51.烹饪料理
  • 扫雷老年版2.0无猜模式
  • postman下载安装和导入导出脚本一键执行
  • 代码随想录算法训练营第29天|134. 加油站、135. 分发糖果、860.柠檬水找零、406.根据身高重建队列
  • 【机器学习】过拟合与欠拟合——如何优化模型性能
  • 【活动】人工智能时代,程序员如何保持核心竞争力?需要掌握哪些技能?
  • 在多态的方法调用中为什么会出现“左边编译左边运行”的现象?多态创建的对象到底是谁属于父类还是子类?通过深扒集合remove方法调用理解其原理
  • CAPL—on signal到底该怎么玩?
  • 消息队列与Kafka集群
  • 海信智能电视的使用心得
  • 旺店通ERP集成金蝶KIS(金蝶KIS主供应链)
  • CSS 实现文本溢出省略号显示,含单行与多行文本溢出
  • ComfyUI - 使用 ComfyUI 部署与测试 FLUX.1 图像生成模型 教程
  • 2024年9月24日---关于MyBatis框架(3)
  • 猫头虎分享:Python库 Falcon 的简介、安装、用法详解入门教程
  • 【好书推荐】《架构真意:企业级应用架构设计方法论与实践》