题解:P6763 [BalticOI 2010] Printed Circuit Board (Day1)
__bilibili_daogu__ · · 题解
分析
转化
考虑两条线段
题目保证所有
简单来说,如果我们把线段按照
形式化地,对于两条线段
例如,对于输入:
1 3
3 1
第一条线段连接
序列问题
根据上述分析,问题转化如下:
将所有线段按底部端点坐标
根据 Dilworth 定理,一个序列最少能划分成的递增子序列的数量,等于其最长不升子序列的长度。因此,我们的目标就是求出排序后序列
由于
实现
我们使用经典的贪心加二分的方法。
设原数组为
因此,原问题转化为求数组
对于 LNDS,我们维护一个数组
最终
细节说明:由于题目保证所有的 std::lower_bound 来求最长上升子序列的长度,这与求最长不降子序列在本题条件下是等价的。
代码
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define bye return 0
#define hello ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define i_ak_all int main()
using namespace std;
i_ak_all{
hello;
ll n;
cin >> n;
vector<ll> b(n), dp;
vector<pair<ll, ll>> a(n);
for (auto & x : a){
cin >> x.first >> x.second;
}
sort(a.begin(), a.end());
for (int i = 0; i < n; i++){
b[i] = -a[i].second;
}
for (ll x : b){
auto it = lower_bound(dp.begin(), dp.end(), x);
if (it == dp.end()){
dp.push_back(x);
}
else {
*it = x;
}
}
cout << dp.size();
bye;
}
AI 使用说明:
本文在写作完成后使用 DeepSeek 进行了润色。