#include <bits/stdc++.h>using namespace std;
#define int long long
queue<int>op;
int a[1000009];
signed main()
{
int m;
int cnt=0;
int n;
cin>>n;
m=n;
while(n)
{
op.push(n);
op.push(op.front());
op.pop();
n-=1;
}
while(op.size())
{
a[++cnt]=op.front();
op.pop();
}
for(int i=m;i>=1;--i)
{
cout<<a[i]<<' ';
}
return 0;
}
P8641
题意:有一圈无序的卡片,上面写着1到n。你可以选任意一个位置开始,然后顺时针数。
1.如果你的数正好等于当前卡片的数字,就把这张卡片拿走,然后从下一张开始重新数1。
2.如果对不上,就继续往下数,直到你再也拿不到卡片为止。把拿走的卡片上的数字加起来就是你的分数。
3.题目要你找出,从哪个位置开始,能得到的分数最高。
思路:我们可以用队列模拟手牌,记录有哪些牌。(其实,也可以暴力)
C++
#include <bits/stdc++.h>using namespace std;
int a[100000];
int main()
{
int maxi=0;
long long n;
cin>>n;
int x=0;
for(int i=1;i<=n;++i)
{
cin>>a[i];
}
for(int i=n+1;i<n*2;++i)
{
a[i]=a[i-n];
}
for(int i=1;i<=n;++i)
{
queue <long long>op;
for(int j=i;j<=i+n-1;++j)
{
op.push(a[j]);
}
int now=0,sum=0;
while(op.size()!=0 && now<=n)
{
now++;
if(op.front()==now)
{
sum+=op.front();
op.pop();
now=0;
}
else
{
op.push(op.front());
op.pop();
}
}
maxi=max(maxi,sum);
}
cout<<maxi;
return 0;
}