AcWing杯 第85场周赛

4791. 死或生

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

int n , ax , ay , bx , by;

int32_t main(){
    cin >> n;
    for( int t , x , y ; n ; n -- ){
        cin >> t >> x >> y;
        if( t == 1 ) ax += x , ay += y;
        else bx += x , by += y;
    }
    if( ax >= ay ) cout << "LIVE\n";
    else cout <<"DEAD\n";

    if( bx >= by ) cout << "LIVE\n";
    else cout <<"DEAD\n";
    return 0;
}

4792. 最大价值

贪心的最大值丢在末尾这样子的收益一定最高。

可以用归纳法证明,令字符串每一位的权值是\(a_i\),最大的权值是\(S\)

则插在其中任意一位\(i\)的增量是\(iS+\sum_{i}^{n} a_i\),一共是\(n+1\)个权值

而插在最后一位的增量是\((n+1)S\),所以插在最后一位一定是最优解

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

int k , w[30] , res , n ;
string s;

int32_t main(){
    cin >> s >> k;
    for( int i = 0 ; i < 26 ; i ++ ) cin >> w[i];
    n = s.size();
    for( int i = 1 ; i <= n ; i ++ ) res += w[ s[i-1] - 'a' ] * i;
    for( int i = 1 ; i < 26 ; i ++ ) w[0] = max( w[0] , w[i] );
    for( int i = n+1 ; i <= n+k ; i ++ ) res += w[0] * i;
    cout << res << "\n";
    return 0;
}

4793. 危险程度

把化学反应当成边,然后按照dfs或者bfs的顺序加入点就好,这样每个连通块就会反应连通块大小减一次。所以就是统计连通块的大小即可,这里我用并查集维护一下就好。

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


vector<int> fa;
int n , res = 1 , m;

int power( int x , int y ){
    int ans = 1 ;
    while( y ){
        if( y&1 ) ans = ans * x;
        y >>= 1 , x = x * x;
    }
    return ans;
}

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

int32_t main(){
    n = read() , m = read();
    fa = vector<int>( n+1 , -1 ) , fa[0] = 0;
    for( int u , v ; m ; m -- )
        u = read() , v = read() , merge( u , v );
    for( int i = 1 ; i <= n ; i ++ )
        if( fa[i] < 0 ) res *= power( 2 , -1-fa[i] );
    cout << res;
    return 0;
}
posted @ 2023-01-09 19:38  PHarr  阅读(16)  评论(0编辑  收藏  举报