Codeforces Round #663 解题报告A~C

题目链接

A、Suborrays

水题。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x7fffffff
const int N = 1e5 + 10;
const double PI= acos(-1.0);
const int mod = 1e9 + 7;
int a[N],vis[N],num[N];
int main()
{
    int t,n;
    cin >> t;
    while( t-- )
    {
        cin >> n;
        //memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++)
        {
            cout << i << " ";
        }
        cout <<endl;
    }
}

B、Fix You

题意: 给出一个n*m的网格,从左上角出发,每个格子里有-个字符(R或D)R代表向右移动,D代表向下移动,问最少改变多少个字符,能让网格中所有位置都能按照字符移动到右下角。(右 下角字符为C这不用管)输出最少改变的字符数量
思路:dfs即可,具体看代码。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const int N = 1e5 + 10;
const double PI= acos(-1.0);
const int mod = 1e9 + 7;
int a[101][101],vis[N],num[N];
char str[101][101];
int minn = inf,m,n;
int sum=0;
void dfs(int x,int y){
    if(a[x][y])
        return;
    a[x][y] = 1;
    if(str[x][y]=='R')
        y+=1;
    else if(str[x][y]=='D')
        x+=1;
    else if(str[x][y]=='C')
        return;
    if(x>n){
        sum++;
        dfs(x-1,y+1);
    }else if(y>m){
        sum++;
        dfs(x+1,y-1);
    }else
        dfs(x,y);
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        sum = 0;
        memset(a,0,sizeof a);
        for(int i=1;i<=n;i++)
            cin >> str[i]+1;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(!a[i][j])
                    dfs(i,j);
        cout << sum <<endl;
    }
}

C、Cyclic Permutations

题意:大体意思是按照他给的规则建图,然后问有几种排序方式能够让这个图有环
思路:其实这个题不需要用到图论的知识。分析可以得出,当一个序列满足先增后减、单调递增或者单调递减时,这个序列构成的图不会含有环。
参考大佬的博客:Cyclic Permutations

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const int N = 1e6 + 10;
const double PI= acos(-1.0);
const int mod = 1e9 + 7;
ll a[N],vis[N],num[N],p[N];
int main() {
    int n;
    cin >> n;
    ll ans = 1;
    a[0] = 1;
    p[0] = 1;
    for(int i = 1;i <= n;i++) {
        a[i] = a[i - 1] * i % mod;
        p[i] = p[i - 1] * 2 % mod;
    }
    ans = (a[n] - p[n - 1] + mod) % mod;
    cout << ans <<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅梦曾倾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值