Codeforces Round #310 (Div. 1) A. Case of Matryoshkas 水题

C. String Manipulation 1.0

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/555/problem/A

Description

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.

The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.

In one second, you can perform one of the two following operations:

    Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
    Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.

According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

Input

The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1.

Output

In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

Sample Input

3 2
2 1 2
1 3

Sample Output

1

HINT

 

题意

 俄罗斯套娃,每秒钟可以把一个娃娃扔进一个大娃娃里面

或者把一个娃娃从大娃娃中拿出来

注意脑补俄罗斯套娃的样子,这个不能从中间断的……

题解:

 只留下从1开始连续的链,其他全拆掉就好了= =

代码

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[20];
const int inf=0x3f3f3f3f;
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

//**************************************************************************************


vector<ll> a[maxn];
int main()
{
    int n=read(),k=read();
    int tmp=0;
    for(int i=0;i<k;i++)
    {
        int t=read();
        for(int j=0;j<t;j++)
        {
            int kiss=read();
            a[i].push_back(kiss);
        }
    }
    ll ans=0;
    for(int i=0;i<k;i++)
    {
        if(a[i][0]==1)
        {
            for(int j=1;j<a[i].size();j++)
            {
                if(a[i][j]==a[i][j-1]+1)
                    ans++;
                else
                    break;
            }
        }
    }
    cout<<(n-1-ans)*2-(k-1)<<endl;
}

 

你可能感兴趣的:(codeforces)