2021中国大学生程序设计竞赛(CCPC)- 网络选拔赛 题解分享 1001/1006/1009

队伍题解分享


题目传送:

1001 Cut The Wire

1006 Power Sum

1009 Command Sequence


目录

Cut The Wire:

Power Sum:

Command Sequence:


Cut The Wire:

解释:队友写的。

#include <bits/stdc++.h>
#define bbn 200005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
typedef  long long int  LL;
using namespace std;
LL t,n,x,num,sum;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        sum=0;
        num=0;
        if(n%2==1)
        {
            if(n%3==0)
            {
                x=n/3;
            }
            else
            {
                x=n/3+1;
            }
            num=(n-x)/2+1;
            sum=num+(n-1)/2+1;
        }
        else
        {
            if(n%3==0)
            {
                x=n/3;
            }
            else
            {
                x=n/3+1;
            }
            
            if(x%2==0)
            {
                num=(n-x)/2;
            }
            else
            {
                num=(n-x)/2+1;
            }
            sum=num+(n+1)/2;

        }
        cout<<sum<<endl;
    }
}

Power Sum:

解释:

由 n^2 - (n-1)^2 = 2*n-1  可以推导出: (n-3)^2 - (n-2)^2 - (n-1)^2 + n^2 = 4

即:连续4个数为“1001”时 ,和为4

易得:1=“1”; 2=“0001”; 3=“01”;

令k=n/4;

n%4==0时;长度为     k*4;   01字符串为: k个“1001”;   

n%4==1时;长度为 1+k*4;   01字符串为:  “1”+k个“1001”;

n%4==2时;长度为 4+k*4;   01字符串为:   “0001”+k个“1001”;

n%4==3时;长度为 2+k*4;   01字符串为:  “01”+k个“1001”;

#include <bits/stdc++.h>
#define bbn 200005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
typedef  long long int  LL;
using namespace std;
int n,t;
void solve(int n)
{
    int k=n/4;
    int m=n%4;
    if(m==0)
    {
        cout<<k*4<<endl;
    }
    else if(m==1)
    {
        cout<<k*4+1<<endl;
       cout<<"1";
    }
    else if(m==2)
    {
         cout<<k*4+4<<endl;
      cout<<"0001";
    }
    else if(m==3)
    {
         cout<<k*4+2<<endl;
      cout<<"01";
    }
    while(k--)
    {
        cout<<"1001";
    }
    cout<<endl;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        solve(n);
    }
}

Command Sequence:

解释:

'U':dy++; 'D':dy--; 'R':dx++; 'L':dx--;

例子:6 URLLDR

步数0123456
dx0010-1-10
dy0111100

步数状态相同 :( dx1=dx2&&dy1==dy2 )

0  6  同一个状态  状态个数:2

1  3   同一个状态  状态个数:2

2     同一个状态  状态个数:1

4     同一个状态  状态个数:1

5      同一个状态  状态个数:1

状态用结构体保存,map存每个状态和相应个数。

//求和 
for(auto i:s)
        {
            LL ans=i.second;//状态数
            sum+=ans*(ans-1)/2;
        }

#include <bits/stdc++.h>
#define bbn 200005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
typedef  long long int  LL;
using namespace std;
struct A
{
    LL x=0;
    LL y=0;
    bool operator<(const A& a) const
    {
        if(y==a.y)
        {
            return x<a.x;
        }
        else
        {
            return y<a.y;
        }
    }
};
LL t,n;
int main()
{
    scanf("%lld",&t);
    while(t--)
    {
        map<A,LL>s;
        LL sum=0;
        A q;
        q.x=0;
        q.y=0;
        s[q]++;
       scanf("%lld",&n);
        for(LL i=1; i<=n; i++)
        {
            LL dxx=0,dyy=0;
            char x;
            cin>>x;
            if(x=='U')
            {
                dyy=1;
            }
            else if(x=='D')
            {
                dyy=-1;
            }
            else if(x=='L')
            {
                dxx=-1;
            }
            else if(x=='R')
            {
                dxx=1;
            }
            q.x+=dxx;
            q.y+=dyy;
            s[q]++;
        }
        for(auto i:s)
        {
            LL ans=i.second;
            sum+=ans*(ans-1)/2;
        }
        printf("%lld\n",sum);
    }
}

总结:

1.第一次参加CCPC网络赛,好菜。。。

 

2.杭电服务器不给力啊。。。

3.


 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值