题目链接:Educational Codeforces Round 99 (Rated for Div. 2)
A. Strange Functions
转换题意就是判断字符串长度
int main()
{
int n;
cin >> n;
while(n--)
{
string s;
cin >> s;
cout << s.length() << endl;
}
}
B. Jumps
操作一:1 + 2 + 3 + 4 + 5 + 6 + 7 + … + k
操作二:(-2)+(-3)+(-4)+(-5)+…+(-k)+(-k-1)
先按操作一去暴力找一个加和不小于x的数y
会发现通过操作二在k步操作内,可以使y变为y-2,y-3,… ,0
很明显如果y比x大1,答案输出暴力步数+1,否则其余的答案就是暴力的步数。
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int x;
scanf("%d",&x);
ll cnt=1,y=0,ans=0;
for(y=0;y<x;) { y+=cnt; cnt++; ans++; }
if(y-x==1) printf("%lld\n",ans+1);
else printf("%lld\n",ans);
}
}
C. Ping-pong
博弈问题,会发现后手拥有主导权,在面对先手打来的球时,后手可选择接与不接,所以后手为了自己赢的多,让先手打x-1个球,这样后手的所有球都能赢,因此最优的是先手拿x-1分,后手拿y分。
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int x,y;
cin >> x >> y;
cout <<x-1 << ' ' << y << endl;
}
}
D. Sequence and Swaps
贪心,因为最后的序列一定是有序的,所以前面逆序的数字一定需要被换。又因为我们换的数字只能是
x
<
a
i
{x<a_i}
x<ai,所以从第一个开始只要
x
<
a
i
{x<a_i}
x<ai我们就换,这样才能保证后面出现逆序的数字换完后一定是有序的。
因此我们就模拟前面的数字能换就换即可。
int a[maxn],b[maxn];
struct Node
{
int num,pos;
}node[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,x;
cin >> n >> x;
int maxx=-1;
a[0]=0;
bool flag=false;
for(int i=1;i<=n;i++)
{
cin >> a[i];
b[i]=a[i];
}
for(int i=1;i<=n;i++)
{
if(a[i]>a[i+1] && i+1<=n) flag=true;
maxx=max(maxx,a[i]);
}
if(!flag)
{
cout << 0 << endl;
continue;
}
sort(b+1,b+1+n);
if(x>maxx && flag)
{
cout << -1 << endl;
continue;
}
int i;
int pos=0;
for(i=1;i<n;i++)
{
if(a[i]>a[i+1]) pos=i;
}
ll ans=0;
for(i=1;i<=pos;i++)
{
if(a[i]>x)
{
swap(a[i],x);
ans++;
}
}
for(i=2;i<=n;i++)
{
if(a[i]<a[i-1]) break;
}
if(i==n+1) cout << ans << endl; else cout << -1 << endl;
}
}