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

AtCoder Beginner Contest 288

时间:2023-02-06 16:34:14浏览次数:48  
标签:AtCoder ch Beginner fa int sum 288 read dp

A - Many A+B Problems

签到

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

#define int long long

int32_t main() {
    int n ;
    cin >> n;
    for( int a , b ; n ; n -- ){
        cin >> a >> b;
        cout << a + b << "\n";
    }
    return 0;
}

B - Qualification Contest

#include <bits/stdc++.h>

using namespace std;

int main(){
    int n , k; cin >> n >> k;
    vector<string> s(k);
    for( auto & i : s ) cin >> i;
    sort( s.begin() , s.end() );
    for( auto i : s ) cout << i << "\n";
    return 0;
}

C - Don’t be cycle

要想不成环,就用并查集维护一下连通性,如果边连接的两点已经联通就删除掉

#include <bits/stdc++.h>

using namespace std;

vector<int> fa;

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

int getfa( int x ){
    if( fa[x] < 0 ) return x;
    return fa[x] = getfa(fa[x]);
}

void merge( int x , int y ){
    x = getfa(x) , y = getfa(y);
    if( x == y ) return;
    if( fa[x] > fa[y] ) swap( x , y );
    fa[x] += fa[y] , fa[y] = x;
}

bool check( int x , int y ){
    x = getfa(x) , y = getfa(y);
    return x == y;
}

int32_t main() {
    int n = read() , m = read() , cnt = 0;
    fa = vector<int>( n+1 , -1 ) , fa[0] = 0;
    for( int u , v ; m ; m -- ){
        u = read() , v = read();
        if( check( u , v ) ) cnt ++;
        else merge( u , v );
    }
    cout << cnt << "\n";
    return 0;
}

D - Range Add Query(补题)

假设\(n=6,k=3,a=\{x,0,0,0,0,0\}\)

第一次操作\(\{0,-x,-x,0,0,0\}\)

第二次操作\(\{0,0,0,x,0,0\}\)

因此可以总结出,每一个数都会加到后面的第k个数上,并且如果是good sequence,操作到最后的k个数一定相同。所以就是维护k个前缀和就好,赛时k个前缀和写的太复杂了,赛后发现Jiangly的写法很简单。

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

int32_t main(){
    int n = read() , k = read();
    vector<int> b(n+1);
    for( int i = 1 ; i <= n ; i ++ ){
        b[i] = read();
        if( i > k ) b[i] += b[i-k];
    }
    for( int l , r , q = read() ; q ; q -- ){
        l = read() , r = read();
        vector<int> c(k , 0 );
        for( int i = r ; i >= r - k + 1 ; i -- ) c[i%k] += b[i];
        for( int i = l-1 ; i >= max( l-k , 0 ) ; i -- ) c[i%k] -= b[i];
        if( c == vector<int>( k , c[0] ) ) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

F - Integer Division(补题)

用\(S[l,r]\)表示字符串中\([l,r]\)构成的十进制数,\(dp_i\)表示前i位分出来的和,我们只需要枚举最后一次是在那里分割的就好

所以\(dp_i=\sum_{j=0}^{i-1}dp_j\times S[j+1,i]\)

然后可以优化转移方程

\[dp_i =\sum_{j=0}^{i-1} dp_j\times S[j+1,i]\\ =\sum_{j=0}^{i-1} dp_j(10S[j+1,j-1] + S[i] )\\ = 10 dp_{i-1} + S[i] \times \sum_{j=0}^{i-1} dp_j \]

这样就可以实现\(O(1)\)的转移

#include <bits/stdc++.h>

using namespace std;

#define int long long
const int mod = 998244353 , N = 2e5+5;

int dp[N] , sum = 0 , n;
string s;

int32_t main(){
    cin >> n >> s;
    dp[0] = 0 , sum = 1;
    for( int i = 1 ; i <= n ; i ++ )
        dp[i] = ( 10 * dp[i-1] + (s[i-1]-'0') * sum ) % mod , sum = ( sum + dp[i] ) % mod;
    cout << dp[n];
    return 0;
}

标签:AtCoder,ch,Beginner,fa,int,sum,288,read,dp
From: https://www.cnblogs.com/PHarr/p/17095818.html

相关文章

  • ABC288 EFG 题解
    E注意到后面选对前面的答案没有影响,而且前面选的顺序对后面的影响是连续的一段(如选2个,那么对应的\(c\)就应该是\(c[i-2..i]\)(对应\(i\)是1、2、3个选时的答案))然......
  • abc288
    C:如果当前连的边和以前连的形成了环,就任意删除一条边,并查集维护。E:首先需要知道,若考虑购买当前物品\(i\),那么设之前买了\(j\)个了,那么可以在\(c_{i-j+1}\simc_i......
  • AtCoder Beginner Contest 288
    A-ManyA+BProblems(abc288a)题目大意给定\(A\),\(B\),输出\(A+B\)。解题思路范围不会爆\(int\),直接做即可。神奇的代码#include<bits/stdc++.h>usingname......
  • ABC 288 ABC
    来水一篇博客,前面虽然打了挺多比赛,但是一直在忙项目和考试,没补题,那些就等补完题目再写完整的题解咯(:水多了也不好哈哈https://atcoder.jp/contests/abc288今天这场断层了......
  • AtCoder Beginner Contest 235 G Gardens
    洛谷传送门考虑不要求每个盒子至少放一个球,答案即为\(\sum\limits_{i=0}^A\binom{n}{i}\times\sum\limits_{i=0}^B\binom{n}{i}\times\sum\limits_{i=0}^C\binom{......
  • AtCoder Beginner Contest 168 C - : (Colon)
    题意:时针转过的角度:​分针转过的角度:。AC代码:constintN=1e6+50;constdoublepi=acos(-1.0);intmain(){doublea,b,h,m;cin>>a>>b>>h>>m;lon......
  • AtCoder Beginner Contest 168 D - .. (Double Dots)
    题意:有个房间,在这些房间中两两连次条边,问除了第一个房间,其他房间走到第一个房间的最短路径,输出这个房间所连的上一个房间,如果走不到,输出.AC代码;constintN=......
  • POJ 2886(线段树+单点修改+约瑟夫环)
    DescriptionN childrenaresittinginacircletoplayagame.Thechildrenarenumberedfrom1to N inclockwiseorder.Eachofthemhasacardwithanon-zer......
  • [ABC266] AtCoder Beginner Contest 266
    比赛链接:Tasks-AtCoderBeginnerContest266先贴代码,题解有空再补。TasksTaskNameTimeLimitMemoryLimitAMiddleLetter2sec1024MBSubmit......
  • Atcoder ABC282F Union of Two Sets
    https://atcoder.jp/contests/abc282/tasks/abc282_fST表板子???这怎么出的?发现要每一个区间都能拆分成至多两个区间,那很明显就能联想到ST表的查询。大概算一下发现......