2024/3/14

B. Little Pony and Sort by Shift

原题链接:Problem - 454B - Codeforces

题目大意:

排序成不递减的

题目做法:

反向去想,只有一个相邻两个不满足的时候才有机会,更具体的还要尾巴比头小。且答案是该位置往后的所有元素就是操作的长度。

AC代码:

#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb(element) push_back(element)
using namespace std;
const int maxn=2e5+10;
void solve()
{
    int n;
    cin>>n;
    int ar[n],times=0,ps=0;
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
        if(i==0) continue;
        if(ar[i]<ar[i-1])
        {
            times++;
            ps=i;
        }
    }
    if(times==0)
    {
        cout<<0<<'\n';
    }
    else if(times==1&&ar[n-1]<=ar[0])
    {
        cout<<n-ps<<'\n';
    }
    else
    {
        cout<<-1<<'\n';
    }
}
signed main()
{
    fast int casen=1;
    //cin>>casen;
	while(casen--) solve();
}
 C. Maximal GCD

原题链接:Problem - C - Codeforces​​​​​​​

题目大意:

把n分解成k个严格递增的数,要求这个数列的gcd最大。

题目做法:

感觉和因数有关系,把因数先弄出来,然后从大到小扫看能不能满足。

好简单的思路,一直在那里爆longlong中间步骤,看来还是要细心才好啊,平白多那么多罚时。。

AC代码:

#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb(element) push_back(element)
#define int long long
using namespace std;
const int maxn=2e5+10;
void solve()
{
    int n,k;
    cin>>n>>k;
    set<int> div;
    for(int i=1;i<=n/i;i++)
    {
        if(n%i==0) div.insert(i),div.insert(n/i);
    }
    auto its=div.end();
    its--;
    while(1)
    {
        if(*its>double(n)*2/k/(k+1))
        {
            if(its==div.begin()) break;
            its--;
            continue;
        }
        int ini=*its;
        for(int j=0;j<k-1;j++)
        {
            cout<<ini<<" ";
            n-=ini;
            ini+=*its;
        }
        cout<<n<<'\n';
        return ;
    }
    cout<<"-1"<<'\n';
}
signed main()
{
    fast int casen=1;
    //cin>>casen;
	while(casen--) solve();
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值