算法做题记录07

Ginger的大花环

        

 思路:只需要选择两种颜色即可完成染色,如果颜色种类少于两种,输出Ginger666。

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[5];
int main()
{
    ll sum=0;
    ll n,k;
    cin>>n>>k;
    for(ll i=0;i<k;i++) cin>>a[i];
    sort(a,a+k);
    if(k==1) cout<<"Ginger666"<<endl;
    else
    {
        sum=a[1]*(n/3);
        sum+=a[0]*((n/3)*2);
        if(n%3>=1) sum+=a[1];
        if(n%3>=2) sum+=a[0];
        cout<<sum<<endl;
    }
    return 0;
}

多米诺骨牌

思路:由于两同方向倾斜的诺骨牌存在一个反方向的诺骨牌,所以初始倾斜的序列只有两种:l r l r...或 r l r l....

代码:

#include<iostream>
using namespace std;
const int N=3e3+10;
char str[N];
int main()
{
    int n,res=0,l=0,f=0;
    cin>>n;
    cin>>str;
    while(l<n&&str[l]=='.') l++;
    if(l==n) res=n;
    else if(str[l]=='L') 
    {
        for(int i=l+1;i<n;i++)
        {
            if(str[i]=='.'&&f==0) res++;
            if(str[i]!='.') f= f==1?0:1;
            if(str[i]=='L'&&i-l>1) res+=(i-l-1)%2;
            if(str[i]=='R') l=i;
        }
    }
    else if(str[l]=='R')
    {
        f=1;
        res=l;
        for(int i=l+1;i<n;i++)
        {
            if(str[i]=='.'&&f==0) res++;
            if(str[i]!='.') f= f==1?0:1;
            if(str[i]=='L'&&i-l>1) res+=(i-l-1)%2;
            if(str[i]=='R') l=i;
        }
    }
    cout<<res<<endl;
    return 0;
}

构造序列

代码:

#include<iostream>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    if((m+1)/2-1>n||n-1>m) cout<<-1<<endl;
    else if(n>=m)
    {
        for(int i=0;i<m;i++) cout<<0<<1;
        if(n>m)cout<<0<<endl;
    }
    else if(m>n)
    {
        while(m>n&&n>0&&m>0)
        {
            cout<<110;
            m-=2;
            n--;
        }
        for(int i=0;i<m;i++)
        {
            if(m) cout<<1;
            if(n) cout<<0;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值