Luo Gu solution to a problem P1684 test

This konjac solution to a problem again made it!

Positive solution to this problem should be greedy

Directly to key topics:

Rhyme may only be a "AABB", "ABAB", "ABBA" and "AAAA" in

We observed rhyme, is not all two 'A' and two 'B';

'AAAA' can be regarded as 'A' and 'B' the same

So is it not a simple greedy yet?

If there are two two identical numbers appear on ans ++

On the code:

#include<bits/stdc++.h>//万能头 
using namespace std;

#define maxn 10010
int n, m, k, a[maxn], f[maxn], c[maxn], x, ans, flag;

int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; ++ i)
{
scanf("%d", &x);//输入 
a[i] = c[i] = x;
}
sort(c + 1, c + n + 1);
m = unique(c + 1, c + n + 1) - c;//去重 
for(int i = 1; i <= n; ++ i)
{
k = lower_bound(c + 1, c + m + 1, a[i]) - c;
a[i] = k;
}
for(int i = 1; i <= n; ++ i)//求解 
{
++ f[a[i]];
if(f[a[i]] == 2) 
{
++ flag;
f[a[i]] = 0;
}
if(flag < 2)
continue; 
flag = 0;
++ ans;
memset(f, 0, sizeof(f));
}
printf("%d", ans);
return 0;
}

Guess you like

Origin www.cnblogs.com/Flash-plus/p/12028335.html
Recommended