Codeforces #310ACase of Matryoshkas(模拟)

来源:互联网 发布:自动发卡密源码 编辑:程序博客网 时间:2024/05/21 08:36

题目链接】click here~~

题目大意】给你n个玩具,规定只能小的玩具套在大的上面,而且是规格依次递增的,比如:1->2->3,求所有玩具套完需要的最小时间花费

解题思路】:只能怪CF时间太晚了,本来前一天熬夜,精神有点疲劳,这次第一题还是赛后补做的,哎~~只能说太虚~~

我的做法:找到序列为1 的,然后依次判断后面的

代码:

#include <bits/stdc++.h>using namespace std;const int N=1e6;int num[N];int n,m,k,t,ans,cnt,res,top;int pre,last,len;int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        int h1=0,res=0;        int q,o,ans;        bool ok=0;        while(m--)        {            scanf("%d",&q);            for(int i=1; i<=q; ++i)            {                scanf("%d",&num[i]);                if(num[i]==1)                {                    ok=1;                }            }            if(ok)            {                h1=1;                for(int i=2; i<=q; ++i)                {                    if((num[i])==(num[i-1]+1))                        h1++;                    else break;                }                res+=q-h1;                ok=0;            }            else            {                res+=q-1;            }        }        res+=n-h1;        printf("%d\n",res);    }    return 0;}
看到别人的一种做法:

思路比较清晰,拿过来借鉴一下~~

/*res:记录最长满足条件即单调递增序列的长度(n-m-res+1):每组玩具假设全部拆解需要的时间(n-res):全部玩具重新套上的时间*/#include <bits/stdc++.h>using namespace std;const int N=1e6;int num[N];int n,m,k,t,ans,cnt,res,top;int pre,last,len;int main(){    cin>>n>>m;    res=0;    for(int i=0; i<m; ++i)    {        cin>>k;        last=len=0;        for(int i=0; i<k; ++i)        {            cin>>num[i];            if(num[i]==last+1)            {                last++;                len++;            }        }        res=max(res,len);    }    printf("%d\n",(n-m-res+1)+(n-res));}/*input3 22 1 21 3output1input7 33 1 3 72 2 52 4 6output10input7 33 1 2 72 3 52 4 6output8*/



0 0
原创粉丝点击