首页 > 其他分享 >AtCoder Beginner Contest 276

AtCoder Beginner Contest 276

时间:2022-11-07 22:02:48浏览次数:56  
标签:AtCoder ch && Beginner int read while 276 getchar

A - Rightmost

#include<bits/stdc++.h>

using namespace std;

int32_t main() {
    string s;
    cin >> s;
    for( int i = s.size() ; i >= 1 ; i -- )
        if( s[i-1] == 'a' ) cout << i , exit(0);
    cout << "-1";
    return 0;
}

B - Adjacency List

#include<bits/stdc++.h>

using namespace std;

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

const int N = 1e5+5;
set<int> e[N];

int32_t main() {
    int n = read() , m = read();
    for( int u , v ; m ; m -- )
        u = read() , v = read() , e[u].insert(v) , e[v].insert(u);
    for( int i = 1 ; i <= n ; i ++ ){
        cout << e[i].size() << " ";
        for( auto it : e[i] )
            cout << it << " ";
        cout << "\n";
    }
    return 0;
}

C - Previous Permutation

直接用prev_permutation

#include<bits/stdc++.h>

using namespace std;

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

const int N = 1e5+5;

int32_t main() {
    int n = read();
    vector<int> ve;
    for( int x ; n ; n -- )
        x = read() , ve.push_back(x);
    prev_permutation( ve.begin() , ve.end() );
    for( auto it : ve )
        cout << it << " ";

}

D - Divide by 2 or 3

每一个数一定可以表示为\(a_i+2b_i+3c_i\)这样的话我们只要判断一下整个序列的\(a_i\)是否相等就可以判断是否可以通过操作是的序列的值变的相等。

然后我们在找出\(b_i,c_i\)的最小值就可以计算出最小操作次数

#include<bits/stdc++.h>

using namespace std;

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

const int N = 1005;
int a[N] ,  b[N] , c[N];

int32_t main() {
    int n = read();
    for( int i = 1 ; i <= n ;  i ++ ) {
        a[i] = read();
        while( a[i] % 2 == 0 ) a[i] /= 2 , b[i] ++;
        while( a[i] % 3 == 0 ) a[i] /= 3 , c[i] ++;
    }
    int minb = INT_MAX , minc = INT_MAX;
    for( int i = 1 ; i <= n ; i ++ ){
        if( a[i] != a[1] )
            cout << "-1\n" , exit(0);
        minb = min( minb , b[i] ) , minc = min( minc , c[i] );
    }
    int res = 0;
    for( int i = 1 ; i <= n ; i ++ )
        res += b[i] - minb + c[i] - minc;
    cout << res << "\n";
}

E - Round Trip

把所有的相邻的道路用并查集合并,然后判断起点周围是否存在一组点是一个集合中的

#include <bits/stdc++.h>

using namespace std;


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n , m;
    cin >> n >> m;
    vector g( n+1 , vector<char>( m+1 ) );
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j <= m ; j ++ )
            cin >> g[i][j];
    vector<int> fa(n*m+1);

    for( int i = 1 ; i <= n*m ; i ++ ) fa[i] = i;
    function<int(int)> getfa = [&]( int x ){
        if( fa[x] == x ) return x;
        return fa[x] = getfa(fa[x]);
    };
    auto merge = [&]( int x , int y ){
        x = getfa(x) , y = getfa(y);
        fa[x] = y;
    };
    auto id = [m](int x , int y ){
        return (x-1)*m+y;
    };
    for( int i = 1 ; i < n ; i ++ )
        for( int j = 1 ; j <= m ; j ++ )
            if( g[i][j] == '.' && g[i+1][j] == '.' )
                merge( id(i,j) , id(i+1,j) );
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j < m ; j ++ )
            if( g[i][j] == '.' && g[i][j+1] == '.' )
                merge( id(i,j) , id(i,j+1) );
    int sx = 0 , sy = 0;
    for( int i = 1 ; i <= n && sx == sy && sx == 0 ; i ++)
        for( int j = 1 ; j <= m && sx == sy && sx == 0 ; j ++ )
            if( g[i][j] == 'S' ) sx = i , sy = j;
    vector<pair<int,int>> d;
    if( sx+1 >= 1 && sx+1 <= n && sy >= 1 && sy <= m ) d.push_back( {sx+1,sy} );
    if( sx-1 >= 1 && sx-1 <= n && sy >= 1 && sy <= m ) d.push_back( {sx-1,sy} );
    if( sx >= 1 && sx <= n && sy+1 >= 1 && sy+1 <= m ) d.push_back( {sx,sy+1} );
    if( sx >= 1 && sx <= n && sy-1 >= 1 && sy-1 <= m ) d.push_back( {sx,sy-1} );

    for( auto [ax,ay] : d )
        for( auto [bx,by] : d){
            if( ax == bx && ay == by ) continue;
            if( getfa( id(ax,ay) ) == getfa( id(bx,by) ) ) cout << "Yes\n" , exit(0);
        }
    cout << "No\n";
    return 0;
}

标签:AtCoder,ch,&&,Beginner,int,read,while,276,getchar
From: https://www.cnblogs.com/PHarr/p/16867627.html

相关文章

  • [数学记录]abc276G Count Sequences
    题意:对满足以下条件的序列计数,膜\(998244353\):\(0\leqa_0\leqa_1\cdots\leqa_n\leqm\)$a_i\not\equiva_j\pmod3$这题的难点在于发现它是简单题。想了......
  • AtCoder Regular Contest 070&071
    ARC070只会个DQAQ,所以就合并到ARC071了。ARC070D-NoNeed给定\(n\)个整数\(a_1\sima_n\),对于\(a_i\),若原来所有包含\(a_i\)且和\(\geK\)的子集去掉\(......
  • pat春季模拟考试+acwing第76周赛+AT276
    pat:模考58分,相较夏季赛差了不少1.模拟给定一个字符串,要求按照得分点和失分点进行模拟,求最后得分即可模拟比较难写参考小柳学渣大神的代码,大神码风和思路都很好1#i......
  • ABC276
    ABC276tasks\(\color{Green}{★}\)表示赛时做出。\(\color{Yellow}{★}\)表示赛后已补。\(\color{Red}{★}\)表示\(\text{Tobesolved}\)。Contestrk:708th......
  • AtCoder Beginner Contest 276
    今天来讲解一下AtCoderBeginnerContest276 C和D传送地址:https://atcoder.jp/contests/abc276一. C-PreviousPermutation题目大意:给你一个有数字1~n组成的序列......
  • AtCoder Beginner Contest 276
    咕咕咕咕。E-RoundTrip如果存在某个点双满足这个点双包含起点且点双大小大于\(4\)则有解。F-DoubleChance考虑不断在之前的基础上在末尾添加一个数,每次更新期......
  • AtCoder Beginner Contest 276 A~G 题解
    今天凌晨CFD题差一句判断AC,晚上ABCG题把插板法和快速幂搞混差点AC。事不过三,再这样一次我直接紫砂。太简单的题就不放翻译和代码了。(事实上这场A-E都是大水题......
  • 【atcoder abc276 】(a* 搜索)
    importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.*;/****@autho......
  • Atcoder Beginner Contest ABC 275 Ex (H) Monster 题解
    先明确\(a_i\)是一个怪物的血量,\(b_i\)是攻击的代价。发现如果我们想攻击一个怪物,不如找出一个极大的包含它的区间,满足这个区间内所有怪物的攻击代价都不大于它本身的代价,......
  • AtCoder Regular Contest 068
    C简单,D有点意思,但没啥用,不写。F有点好玩,想了一个小时,看了一上午题解,弄明白了。E-SnukeLine\(n\)个物品,第\(i\)个物品在\([l_i,r_i]\)间。你初始在第0格,......