题目大意:给出N只老鼠的体重和速度
要求你找出最多的老鼠,这些老鼠满足体重递增,速度递减
解题思路:DAG裸题
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
const int INF = 0x3f3f3f3f;
struct Mouse{
int id, weight, speed;
}mouse[N];
int dp[N], path[N];
int n = 0, id = 1;
bool cmp(const Mouse &a, const Mouse &b) {
if (a.weight == b.weight)
return a.speed > b.speed;
else
return a.weight < b.weight;
}
void out(int mark) {
if (path[mark] == 0) {
printf("%d\n", mouse[mark].id);
return ;
}
out(path[mark]);
printf("%d\n", mouse[mark].id);
}
void solve(){
sort(mouse, mouse + n, cmp);
path[0] = -1;
dp[0] = 0;
int Max = 0, mark = 0;;
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (mouse[i].weight > mouse[j].weight && mouse[i].speed < mouse[j].speed && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
path[i] = j;
if (dp[i] > Max) {
Max = dp[i];
mark = i;
}
}
printf("%d\n", Max);
out(mark);
}
int main() {
while (scanf("%d%d", &mouse[n].weight, &mouse[n].speed) != EOF) {
mouse[n++].id = id++;
}
mouse[n].weight = -1; mouse[n].speed = INF; mouse[n++].id = 0;
solve();
return 0;
}