Codeforces Round #564 (Div. 2) C. Nauuo and Cards

时间:2023-03-10 03:19:20
Codeforces Round #564 (Div. 2) C. Nauuo and Cards

链接:https://codeforces.com/contest/1173/problem/C

题意:

Nauuo is a girl who loves playing cards.

One day she was playing cards but found that the cards were mixed with some empty ones.

There are nn cards numbered from 11 to nn, and they were mixed with another nn empty cards. She piled up the 2n2n cards and drew nn of them. The nn cards in Nauuo's hands are given. The remaining nn cards in the pile are also given in the order from top to bottom.

In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.

Nauuo wants to make the nn numbered cards piled up in increasing order (the ii-th card in the pile from top to bottom is the card ii) as quickly as possible. Can you tell her the minimum number of operations?

思路:

先考虑能不能直接出完。

再考虑出几个0之后,一起把n张牌出完。

假设第i张牌再第二个数组中的位置是pi,可以得到只有当其处在i-1这个位置时。才能一下n步出完。

得到res = max(res, pi-i+1+n).

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
int n, m, k, t; map<int, int> In;
int a[MAXN]; int main()
{
// freopen("test.in", "r", stdin);
cin >> n;
for (int i = 1;i <= n;i++)
cin >> a[i], In[a[i]] = 0;
for (int i = 1;i <= n;i++)
cin >> a[i], In[a[i]] = i;
if (In[1])
{
int i = 2;
for (;In[i] == In[i-1]+1;i++);
if(In[i-1] == n)
{
int j;
for (j = i;j <= n && In[j] <= j-i;j++);
if (j > n)
{
cout << n-i+1 << endl;
return 0;
}
}
}
int res = 0;
for (int i = 1;i <= n;i++)
res = max(res, In[i]-i+1+n);
cout << res << endl; return 0;
}