Codeforces Round #259(454A,454B,453A(排列组合))

本文解析了 CodeForces 上的三道经典题目,包括 LittlePonyandCrystalMine、LittlePonyandSortbyShift 和 LittlePonyandExpectedMaximum。通过详细的解题思路和示例代码,帮助读者理解并掌握相关算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Little Pony and Crystal Mine


题目链接:

http://codeforces.com/problemset/problem/454/A

解题思路:

Brief description:

Draw the grid graph as the problem said.

Analysis:

Just a few basics of your programming language. It's easy.

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
    int n;
    while(~scanf("%d",&n)){
        int tmp1 = n/2,tmp2 = 1,cnt = n/2+1;
        for(int i = 1; i <= cnt; i++){
            for(int j = 1; j <= tmp1; j++)
                printf("*");
            for(int j = 1; j <= tmp2; j++)
                printf("D");
            for(int j = 1; j <= tmp1; j++)
                printf("*");
            tmp1--;
            tmp2 += 2;
            printf("\n");
        }
        tmp1++;
        tmp2 -= 2;
        for(int i = cnt-1; i >= 1; i--){
            tmp1++;
            tmp2 -= 2;
            for(int j = 1; j <= tmp1; j++)
                printf("*");
            for(int j = 1; j <= tmp2; j++)
                printf("D");
            for(int j = 1; j <= tmp1; j++)
                printf("*");
            printf("\n");
        }
    }
    return 0;
}

2.Little Pony and Sort by Shift


题目链接:

http://codeforces.com/problemset/problem/454/B

解题思路:

Brief description:

Ask the minimum unit shift you need to sort a array.

Analysis:

Just a few basics of your programming language. It's not hard.

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
    int n;
    while(~scanf("%d",&n)){
        int x0,x,tmp = 0,down = 0,pos = n;
        scanf("%d",&x0);
        tmp = x0;
        for(int i = 1; i < n; i++){
            scanf("%d",&x);
            if(down > 1)
                continue;
            if(x < tmp){
                down++;
                pos = i;
            }
            tmp = x;
        }
        if(down > 1 || down == 1 && x > x0)
            printf("-1\n");
        else
            printf("%d\n",n-pos);
    }
    return 0;
}

3.Little Pony and Expected Maximum(排列组合)


题目链接:

http://codeforces.com/problemset/problem/453/A

解题思路:

Brief description:

Calculate the expected maximum number after tossing a m faces dice n times.

Analysis:

Take m = 6, n = 2 as a instance.

6 6 6 6 6 6
5 5 5 5 5 6
4 4 4 4 5 6
3 3 3 4 5 6
2 2 3 4 5 6
1 2 3 4 5 6

Enumerate the maximum number, the distribution will be a n-dimensional super-cube with m-length-side. Each layer will be a large cube minus a smaller cube. So we have:

Calculate in may cause overflow, we could move the divisor into the sum and calculate (i / m)n instead.

化简如下:

 p = ( (1^n-0^n)*1 + (2^n-1^n)*2 。。。 +(m^n - (m-1)^n)*m ) / m^n  =》p = m - ((m-1)/m)^n + ((m-2)/m)^n。。。+(1/m)^n ;

AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int main (){
    int n,m;
    scanf("%d%d",&m,&n);
    double ans = 0;
    for (int i = 1; i < m; i++)
        ans += pow((double)i/m,n);
    printf("%.12lf\n",(double)m - ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值