20220201模拟赛题解

B2142 求 1+2+3+...+N 的值

就是用高斯公式

#include <bits/stdc++.h>
using namespace std;

int n;

int main()
{
    cin >> n;
    cout << (n+1)*n/2 << endl;
}

B2112 石头剪子布

没有什么好办法就是按照题目模拟即可

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    string a , b;
    cin >> n;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> a >> b;
        if( a == b ) printf("Tie\n");
        else
        {
            if( (a == "Rock" && b == "Scissors" ) || (a == "Scissors" && b == "Paper" ) || ( a == "Paper" && b == "Rock" ) ) printf("Player1\n");
            else printf("Player2\n");
        }
    }
}

B2113 输出亲朋字符串

char的相加就是ascall码的相加

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    string s;
    cin >> s;
    n = s.size();
    for( int i = 1 ; i < n ; i ++ )
        printf("%c" , s[i] + s[i-1] );
    printf("%c\n" , s[n-1] + s[0] );
}

B2117 整理药名

用python内置的函数即可

n = int( input() )
for i in range( 0 , n ):
    s = input()
    print( s[0:1].upper()+s[1:].lower() )

B2131 甲流病人初筛

模拟

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n , cnt = 0;
    string s;
    double x;
    bool f;
    cin >> n;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> s >> x >> f;
        if( x >= 37.5 && f )
        {
            cout << s << "\n";
            cnt ++;
        }
    }
    cout << cnt << endl;
}

B2130 简单算术表达式求值

模拟

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 105;


int main()
{
    ll a , b;
    char op;
    cin >> a >> op >> b;
    if( op == '+' ) cout << a + b  << endl;
    else if( op == '-' ) cout << a - b << endl;
    else if( op == '*' ) cout << a * b << endl;
    else if( op == '/' ) cout << a / b << endl;
    else cout << a % b << endl;

}

B2103 图像相似度

遍历一边即可

#include <bits/stdc++.h>
using namespace std;

const int N = 105;
int n , m , cnt;
bool f[N][N];

int main()
{
    cin >> n >> m;
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j <= m ; j ++ )
            cin >> f[i][j];
    bool p;
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j <= m ; j ++ )
        {
            cin >> p;
            if( p == f[i][j] ) cnt ++;
        }
    printf("%.2lf" , 100.0 * cnt / (n*m) );
    
}

B2144 阿克曼(Ackmann)函数

记忆化搜索

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 105;
int f[N][N];

int ak( int m , int n )
{
    if( f[m][n] ) return f[m][n];
    if( m == 0 ) return f[m][n] = n + 1;
    if( m > 0 && n == 0 ) return f[m][n] = ak(m-1,1);
    return f[m][n] = ak( m-1 , ak(m,n-1) );
}

int main()
{
    int n , m;
    cin >> n >> m;
    cout << ak(n,m) << endl;
}

P3392 涂国旗

枚举分界处即可

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 55;
int r[N] , b[N] , w[N] , cnt , res , ans = N*N;
string s;

int main()
{
    int n , m;
    cin >> n >> m;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> s;
        for( auto it : s )
        {
            if( it == 'W' ) w[i] ++;
            else if( it == 'B' ) b[i] ++;
            else r[i] ++;
        }
    }
    for( int i = 1; i < n - 1 ; i ++ )
        for( int j = i + 1 ; j < n ; j ++ )
        {
            cnt = 0;
            for( int k = 1 ; k <= i ; k ++ ) cnt += m - w[k];
            for( int k = i+1 ; k <= j ; k ++ ) cnt += m - b[k];
            for( int k = j+1 ; k <= n ; k ++ ) cnt += m - r[k];
            ans = min( ans , cnt );
        }
    cout << ans << endl;
}

P3692 夏幻的考试

大模拟

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 55;
int t , n , p[N] , ID , cnt;
char type;
string s;

void slove()
{
    cin >> s;
    type = s[15];
    ID = 0;
    for( int i = 0 ; i <= 15 ; i ++ ) ID = (ID << 1) + ( s[i] == '1' );
    if( ID > 10000 || ID < 1 )
    {
        printf("Wrong ID\n\n");
        for( int i = 0 ; i <= n ; i ++ ) cin >>s;
        return ;
    }
    printf("ID: %d\n" , ID );
    cin >> s;
    if( s[0] == s[1] ) printf("Type Incorrect\n");
    else if( s[1] == type ) printf("Type Correct\n");
    else printf("Type Incorrect\n");

    cnt = 0;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> s;
        bitset<4>ss(s);
        if( ss.count() == 1 && s[p[i]] == '1' ) cnt ++;
    }
    printf("%.1lf\n\n" , 100.0 * cnt / n );

}

int main()
{
    cin >> t >> n >> s;
    for( int i = 1 ; i <= n ; i ++ ) p[i] = s[i-1] - 'A';
    while( t-- ) slove();

}

P2404 自然数的拆分问题

用dfs枚举每一数就行,注意当前的数要大于上一个数

#include <bits/stdc++.h>
using namespace std;

const int N = 10;
int n , a[N];

void dfs( int t , int k , int sum )
{
    if( sum == n && t > 1 )
    {
        for( register int i = 1 ; i < t ; i ++ ) printf( "%d+" , a[i] );
        printf( "%d\n" , a[t] );
        return ;
    }
    if( sum > n ) return ;
    for( register int i = k ; i + sum <= n ; i ++ )
    {
        a[ t + 1 ] = i;
        dfs( t + 1 , i , sum + i );
    }
    return ;
}

int main()
{
    cin >> n;
    dfs( 0 , 1 , 0 );
    return 0;
}

P2952 [USACO09OPEN]Cow Line S

用双端队列来模拟就行了

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 55;
deque<int> q;
int n;

inline int read()
{
    int x = 0 , ch = getchar();
    while( ch < '0' || ch > '9' ) ch = getchar();
    while( ch >= '0' && ch <= '9' ) x = ( x << 3 ) + ( x << 1 ) + ch - '0' , ch = getchar();
    return x;
}

int main()
{
    n = read();
    char op , f;
    for( int i = 1 , x ; n ; n -- )
    {
        cin >> op >> f;
        if( op == 'A' )
        {
            if( f == 'L') q.push_front(i);
            else q.push_back(i);
            i ++;
        }
        else
        {
            x = read();
            if( f == 'L' )
                while( x -- ) q.pop_front();
            else
                while( x -- ) q.pop_back();
        }
    }
    for( auto it : q )
        printf("%d\n" , it);
    return 0;
}

P1122 最大子树和

从一做根节点,然后遍历一边就好了

#include <bits/stdc++.h>
using namespace std;


const int N = 16005 , INF = 0x7f7f7f7f;
int n , v[N] , head[N] ,f[N] , ans;
vector<int> e[N];
bool vis[N];


inline int read()
{
    register int x = 0 , f = 1;
    register char ch = getchar();
    while( ch < '0' || ch > '9' )
    {
        if( ch == '-' ) f = -1;
        ch = getchar();
    }
    while( ch >= '0' && ch <= '9' )
    {
        x = ( x << 3 ) + ( x << 1 ) + ch - '0';
        ch = getchar();
    }
    return x * f;
}
inline void dfs( int x )
{
    f[x] = v[x] , vis[x] = 1;
    for( register auto it : e[x] )
    {
        if( vis[it] ) continue;
        dfs( it );
        f[x] = max( f[x] , f[x] + f[it] );
    }
    return ;
}

int main()
{
    n = read();
    for( register int i = 1 ; i <= n ; v[i] = read() , i ++ );
    for( register int i = 1 , u , v ; i < n ; i ++ )
    {
        u = read() , v = read();
        e[u].push_back(v) , e[v].push_back(u);
    }
    dfs( 1 );
    ans = -INF;
    for( register int i = 1 ; i <= n ; i ++ ) ans = max( ans , f[i] );
    cout << ans << endl;
    return 0;
}

P2697 宝石串

分别用两个前缀和数组来判断宝石数时候相等,对于长度可以用二分来枚举

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e6+5;
int a[N] , b[N];
int n;
string s;

bool check( int x )
{
    for( int i = x ; i <= n ; i ++ )
        if( (a[i] - a[i-x]) == ( b[i]-b[i-x] ) ) return 1;
    return 0;
}

int main()
{
    cin >> s;
    n = s.size();
    for( int i = 1 ; i <= n ; i ++ )
    {
        a[i] = a[i-1] , b[i] = b[i-1];
        if( s[i-1] == 'R' ) a[i]++;
        else b[i] ++;
    }
    int l = 1 , r = min( a[n] , b[n] ) , mid , res = 0;
    while( l <= r )
    {
        mid = ( l + r ) >> 1;
        if( check( mid*2 ) ) res = mid , l = mid + 1;
        else r = mid - 1;
    }
    cout << res * 2 << endl;
}

P2781 传教

虽然题目看起来像一个线段树的模版题,但是由于n很大需要进行离散化

但是因为查询数很少,我们可以把所有添加的区间存起来,当需要查询的时候可以把所有的区间遍历一边,然后找出交集即可

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e3+5;
ll a[N] , b[N] , w[N] , cnt ;
ll n , m;
ll sum;

inline int read()
{
    int x = 0 , ch = getchar();
    while( ch < '0' || ch > '9' ) ch = getchar();
    while( ch >= '0' && ch <= '9' ) x = ( x << 3 ) + ( x << 1 ) + ch - '0' , ch = getchar();
    return x;
}

int main()
{
    n = read() , m = read();
    for( ll op , l , r ; m ; m -- )
    {
        op = read();
        if( op == 1 )
            cnt ++ , a[cnt] = read() , b[cnt] = read() , w[cnt] = read();
        else
        {
            l = read() , r = read() , sum = 0;
            for( int i = 1 ; i <= cnt ; i ++ )
            {
                if( a[i] > r || b[i] < l ) continue;
                sum += ( min( r , b[i] ) - max( l , a[i] ) + 1 ) * w[i];
            }
            printf("%lld\n" , sum );
        }
    }
}
posted @ 2022-03-02 20:19  PHarr  阅读(157)  评论(0编辑  收藏  举报