导弹拦截
被19年薄纱了。
嗯造两个小时,44pts。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N], cnt = 0, sum = 0, ans = 0;
int F[N], tag[N];
bool vis[N], is_first = true;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
while (cin >> a[++cnt])
F[cnt] = 1;
cnt--;
while (sum < cnt)
{
for (int i = 1; i <= cnt; i++)
{
F[i] = 1;
}
for (int i = 1; i <= cnt; i++)
{
if (vis[i])
continue;
for (int j = 1; j < i; j++)
{
if (vis[j])
continue;
if (a[i] <= a[j] && F[i] < F[j] + 1)
{
F[i] = F[j] + 1;
tag[i] = j;
}
}
}
int res = 0, p = 0;
for (int i = 1; i <= cnt; i++)
{
if (F[i] > res)
{
res = F[i];
p = i;
}
}
if (is_first)
cout << res << endl, is_first = false;
sum += res;
for (int i = 1; i <= res; i++)
{
vis[p] = true;
p = tag[p];
}
ans++;
}
cout << ans;
return 0;
}
思路特别暴力,就是求最长上升子序列,然后直接把这个系统直接用来算第二问。
理论上应该能对,但只有44pts,没想出为什么。
看了题解之后突然记起来可以用lis求第二问,自己能写出100分代码,但是优化完全没思路,线段树和树状数组也都还没复习,待填坑。
\(100pts\)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N], cnt = 0;
int F[N], F1[N];
int main()
{
while (cin >> a[++cnt])
F[cnt] = F1[cnt] = 1;
cnt--;
for (int i = 1; i <= cnt; i++)
{
for (int j = 1; j < i; j++)
{
if (a[i] <= a[j] && F[i] < F[j] + 1)
{
F[i] = F[j] + 1;
}
}
}
for (int i = 1; i <= cnt; i++)
{
for (int j = 1; j < i; j++)
{
if (a[i] > a[j] && F1[i] < F1[j] + 1)
{
F1[i] = F1[j] + 1;
}
}
}
int res = 0;
for (int i = 1; i <= cnt; i++)
{
if (F[i] > res)
{
res = F[i];
}
}
cout << res << endl;
res = 0;
for (int i = 1; i <= cnt; i++)
{
if (F1[i] > res)
{
res = F1[i];
}
}
cout << res << endl;
return 0;
}
标签:F1,cnt,cout,int,res,导弹,2023,拦截,include
From: https://www.cnblogs.com/kdlyh/p/17825607.html