题目链接:点击这里
纠结了一上午,晚上才瞥到这个条件“Xi 互不相等且 Yi 互不相等” T^T
将木板按照 Xi 从小到大排序,将这时的 Yi 记为 Zi 数列,则问题变成将 Zi 划分为尽可能少的若干组上升子序列,即求最长上升子序列的个数。
根据Dilworth定理,最长上升子序列的个数等于最长不上升子序列的长度。
借助 的算法,在求出最小组数的同时得出分组方案。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 100010;
struct node {
int x, y;
int id;
}a[maxn];
int dp[maxn], ans[maxn];
bool cmp(node a, node b)
{
return a.x < b.x;
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
{
scanf("%d%d", &a[i].x, &a[i].y);
a[i].id = i;
}
sort(a+1, a+1+n, cmp);
int len = 1;
dp[1] = a[1].y;
ans[a[1].id] = 1;
for(int i = 2; i <= n; ++i)
{
if(a[i].y<dp[len])
{
dp[++len] = a[i].y;
ans[a[i].id] = len;
}
else
{
int pos = upper_bound(dp+1, dp+1+len, a[i].y, greater<int>()) - dp;
//printf("**%d\n", pos);
dp[pos] = a[i].y;
ans[a[i].id] = pos;
}
}
printf("%d\n", len);
for(int i = 1; i <= n; ++i)
printf("%d ", ans[i]);
return 0;
}
来源:CSDN
作者:菜是原罪QAQ
链接:https://blog.csdn.net/qq_42815188/article/details/104348373