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();
}