codeforces 555a//Case of Matryoshkas// Codeforces Round #310(Div. 1)

题意:1-n的数字,大的在小的后面,以这种规则已经形成的几个串,现在要转为一个串,可用的操作是在末尾拆或添加,问要操作几次?

模拟了很久还是失败,看题解才知道是数学。看来这种只要结果的题,模拟很不合算。

设结果为res,res先为0,看1在的那个串,在后一位不等于前一位加1的地方断开。res+=剩下数量*2,因为剩下的都要拆掉,然后在某个时刻再装上去。对于其他的串,res+=(串长度-1)*2+1,因为最前面的数字不用拆,只要装。

乱码:

#pragma comment(linker,"/STACK:1024000000,1024000000") 
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
using namespace std;
const int SZ=1e5+10,INF=0x7FFFFFFF;
typedef long long lon;
vector<int> vct[SZ];
int belong[SZ];
bool sep[SZ];


int main()
{
    std::ios::sync_with_stdio(0);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<k;++i)
    {
        int m;
        cin>>m;
        for(int j=0;j<m;++j)
        {
            int tmp;
            cin>>tmp;
            vct[i].push_back(tmp);
            belong[tmp]=i;
        }
    }
    int id=belong[1];
    int res=0;
    for(int i=0;i<vct[id].size();++i)
    {
        if(vct[id][i]!=i+1)
        {
            res+=vct[id].size()-i;
            res+=vct[id].size()-i;
            break;
        }
    }
    for(int i=0;i<k;++i)
    {
        if(i!=id)res+=(vct[i].size()-1)*2+1;
    }
    cout<<res<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/gaudar/p/9648490.html