首页 > 其他分享 >AcWing杯 第85场周赛

AcWing杯 第85场周赛

时间:2023-01-09 19:44:18浏览次数:38  
标签:周赛 ch int cin long fa read 85 AcWing

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

标签:周赛,ch,int,cin,long,fa,read,85,AcWing
From: https://www.cnblogs.com/PHarr/p/17038367.html

相关文章

  • ACWING 4261. 孤独的照片
    题意:给一个长度为n的只包含'G'和'H'字符串要求统计所有长度>=3且只有一个不一样的字符的子串个数思路:最开始的思路就是找到一个满足的三个字符然后向两边扩展然后......
  • ACWING 4645. 选数异或
    url:4645.选数异或-AcWing题库题意:给n个数,再给m次查询,给一个数x每次询问给一个区间l,r,问是否能从$[l,r]$中选出两个下标不同的数使得它们的异或值等于x 思路......
  • LeetCode_单周赛_327
    目录6283.正整数和负整数的最大计数代码6285.执行K次操作后的最大分数代码6284.使字符串总不同字符的数目相等代码6283.正整数和负整数的最大计数代码直接遍历统......
  • ACWING 4366. 上课睡觉
    url:4366.上课睡觉-AcWing题库题意:给n个石堆,相邻石堆可以合并现在要求每个石堆都相等,问最少合并多少次思路:由于不管咋个合并,石子数是不会变的那么就可以枚举......
  • [2185] LeetCode 刷题笔记: 统计包含给定前缀的字符串 [s]
    [2185]LeetCode刷题笔记:统计包含给定前缀的字符串[s]目录[2185]LeetCode刷题笔记:统计包含给定前缀的字符串[s]题目描述题解参考简单模拟复杂度分析参考题解C/C++......
  • AcWing第85场周赛
    这场周赛是手速局hh死或生某国正在以投票的方式决定2名死刑犯(编号1∼2)的生死。共有n组人员(编号1∼n)参与投票,每组10人。每组成员只参与一名死刑犯的投票,其中第......
  • leetcode-2185. 统计包含给定前缀的字符串
    简单题,重拳出击funcprefixCount(words[]string,prefstring)int{validCnt:=0for_,w:=rangewords{notValid:=falseiflen(w)......
  • 2185. 统计包含给定前缀的字符串
    2185.统计包含给定前缀的字符串classSolution{publicintprefixCount(String[]words,Stringpref){intres=0;for(Stringword:words......
  • leetcode-485-easy
    MaxConsecutiveOnesGivenabinaryarraynums,returnthemaximumnumberofconsecutive1'sinthearray.Example1:Input:nums=[1,1,0,1,1,1]Output:3E......
  • 周赛题目证明+at数学题
    题目链接:https://www.acwing.com/problem/content/4795/比赛的时候感觉插入到最后面是最大的,当时只是感觉并没严格证明,y总说比赛的时候可以不用严格证明,赛后要这样做,这样......