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

猜你喜欢

转载自blog.csdn.net/qq_45949914/article/details/107924963