首页 > 其他分享 >Codeforces Round 911 (Div. 2)

Codeforces Round 911 (Div. 2)

时间:2023-12-04 19:13:46浏览次数:33  
标签:int Codeforces len back long ans Div 911 dp

Codeforces Round 911 (Div. 2)

A. Cover in Water

,,,mc无限水

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

void solve(){
    int n;
    string s;
    cin>>n>>s;
    s = " " + s + " ";
    int cnt=0;
    for(int i=2;i<=n-1;i++){
        if(s[i]=='.'&&s[i-1]=='.'&&s[i+1]=='.'){
            cout<<2<<endl;
            return;
        }
        if(s[i]=='.') cnt++;
    }
    if(n!=1)cnt+=(s[1]=='.')+(s[n]=='.');
    else    cnt+=(s[1]=='.');
    cout<<cnt<<endl;
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T=1;
	cin>>T;
	while(T--) solve();
}

B. Laura and Operations

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

void solve(){
    int a,b,c;
    vector<int> ans;
    cin>>a>>b>>c;
    int len=abs(b-c);
    if(len&1) ans.push_back(0);
    else      ans.push_back(1);
    len=abs(a-c);
    if(len&1) ans.push_back(0);
    else      ans.push_back(1);
    len=abs(a-b);
    if(len&1) ans.push_back(0);
    else      ans.push_back(1);
    for(auto u:ans) cout<<u<<" ";
    cout<<endl;
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T=1;
	cin>>T;
	while(T--) solve();
}

C

比B简单

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

const int N = 3e5 + 10;
int l[N],r[N];
int len[N];
int n;
string s;
void dfs(int u){
    if(l[u]==0&&r[u]==0) return;
    if(s[u]=='L')     len[l[u]]=len[u],len[r[u]]=len[u]+1;
    else if(s[u]=='R')len[r[u]]=len[u],len[l[u]]=len[u]+1;
    else len[r[u]]=len[u]+1,len[l[u]]=len[u]+1;
    if(l[u]!=0) dfs(l[u]);
    if(r[u]!=0) dfs(r[u]);
}
void solve(){
    cin>>n;
    cin>>s;
    s = " " + s;
    vector<int> leaf;
    for(int i=1;i<=n;i++){
        cin>>l[i]>>r[i];
        if(l[i]==0&&r[i]==0) leaf.push_back(i);
    }
    len[1]=0;
    dfs(1);
    int ans=1e18;
    for(auto u:leaf) ans=min(ans,len[u]);
    cout<<ans<<endl;
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T=1;
	cin>>T;
	while(T--) solve();
}

D

有种再写一遍又不会写的感觉。

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

const int N = 1e5 + 10;
int a[N];
int n;

void solve(){

    vector<vector<int>> factor(N+10);
    for(int i=1;i<=N;i++)
        for(int j=i;j<=N;j+=i)
            factor[j].push_back(i);

    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);

    vector<vector<int>> c(N+10);
    for(int i=1;i<=n;i++)
        for(auto u:factor[a[i]])
            c[u].push_back(n-i);

    vector<int> f(N+10);
    vector<int> g(N+10);
    for(int i=1;i<=N;i++)
        for(int j=0;j<c[i].size();j++)
            f[i] += c[i][j]*j;
    int ans=0;
    for(int i=N;i>0;i--){
        g[i]=f[i];
        for(int j=i*2;j<=N;j+=i) g[i] -= g[j];
        ans += g[i]*i;
    }
    cout<<ans<<endl;
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T=1;
	cin>>T;
	while(T--) solve();
}

E

缩点+拓扑

md没有初始化G,找错找半天。

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

const int N = 2e5 + 10;
vector<int> G[N];
int a[N];
int n,m;
int dfn[N] , low[N] ,dfs_clock;
bool vis[N];
int fa[N] , siz[N] , sum[N] , du[N];
int tot;
vector<pair<int,int>> dp(N);
vector<int> path;
void tarjin(int u){
    dfn[u]=low[u]=++dfs_clock;
    vis[u]=true;
    path.push_back(u);
    for(auto v:G[u]){
        if(!dfn[v]){
            tarjin(v);
            low[u]=min(low[v],low[u]);
        }else if(vis[v]) low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        int y;
        tot++;
        do{
            y=path.back();
            path.pop_back();
            vis[y]=false;
            siz[tot]++;
            fa[y]  =  tot;
            sum[tot] += a[y];
        }while(y!=u);
    }
}

void solve(){
    dfs_clock=tot=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        dfn[i]=low[i]=0;
        sum[i]=du[i]=0;
        dp[i].first=dp[i].second=0;
        fa[i]=i;
        siz[i]=0;
        vis[i]=false;
        G[i].clear();	//这里害我卡了一个小时
    }
    vector<pair<int,int>> edge(m);
    for(auto &[u,v]:edge){
        cin>>u>>v;
        G[u].push_back(v);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])
            tarjin(i);

    for(int i=1;i<=n;i++) G[i].clear();
    for(auto [u,v]:edge){
        if(fa[u]==fa[v]) continue;
        G[fa[u]].push_back(fa[v]);
        du[fa[v]]++;
    }

    queue<int> que;
    for(int i=1;i<=tot;i++){
        if(du[i]==0){
            que.push(i);
            dp[i].first=siz[i];
            dp[i].second=sum[i];
        }
    }
    while(que.size()){
        int u=que.front();que.pop();
        for(auto v:G[u]){
            if(dp[u].first+siz[v]>dp[v].first){
                dp[v].first=dp[u].first+siz[v];
                dp[v].second=dp[u].second+sum[v];
            }else if(dp[u].first+siz[v]==dp[v].first){
                dp[v].second=min(dp[u].second+sum[v],dp[v].second);
            }
            if(--du[v]==0) que.push(v);
        }
    }
    int len=0;
    int all=0;
    for(int i=1;i<=n;i++){
        if(dp[i].first>len){
            len=dp[i].first;
            all=dp[i].second;
        }else if(dp[i].first==len)
            all=min(all,dp[i].second);
    }
    cout<<len<<" "<<all<<endl;
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T=1;
	cin>>T;
	while(T--) solve();
}

标签:int,Codeforces,len,back,long,ans,Div,911,dp
From: https://www.cnblogs.com/zfxyyy/p/17875709.html

相关文章

  • Codeforces Round 912 (Div. 2)
    CodeforcesRound912(Div.2)什么位运算专场A.HalloumiBoxes#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;inta[110];intn,k;voidsolve(){cin>>n>>k;for(inti=0;i<n;i++)cin&......
  • Educational Codeforces Round 158 (Rated for Div. 2)
    EducationalCodeforcesRound158(RatedforDiv.2)AEDU的题总是感觉写起来怪怪的#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;inta[101];voidsolve(){intn,x;cin>>n>>x;intans=0;......
  • Codeforces Round 912 (Div. 2)补题B、C、D1
    CodeforcesRound912(Div.2)B.StORageroom思路\(a_i\)=\(M_i\)\(_1\)&\(M_i\)\(_2\)&\(M_i\)\(_3\)&...&\(M_i\)\(_n\)\((i!=j)\)ac代码#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong......
  • CF1901 C Add, Divide and Floor 题解
    LinkCF1901CAdd,DivideandFloorQuestion给定一个长度为\(n\)的序列,每次操作你需要选择一个整数\(x\),并将所有\(a_i\)替换为\(\lfloor\frac{a_i+x}{2}\rfloor\)。求至少多少次操作后能将所有\(a_i\)变相同若最少次数小于等于\(n\),输出操作次数和每次操作所选......
  • Educational Codeforces Round 159 (Rated for Div. 2)
    EducationalCodeforcesRound159(RatedforDiv.2)基本情况A题秒了。B题想出来贪心思想,也想出来怎么找最优解了,但实现极其复杂繁琐,最后以先超时优化后又错误的结果告终。B.GettingPoints明显越后面开始学收益越高。然后写了个简单粗暴的纯模拟,T了。#include<iostrea......
  • CodeForces 1900F Local Deletions
    洛谷传送门CF传送门操作没有什么性质,唯一一个性质是,操作次数不超过\(\logn\)(每次至多保留一半元素)。于是我们可以直接模拟操作。但是肯定不能直接模拟。考虑先对原序列模拟一次,求出经过\(i\)次操作后保留的位置集合\(S_i\)。那么只保留\([l,r]\)的元素,可能会造成端点......
  • Codeforces Round 911 (Div. 2)
    Preface忙里偷闲补一下之前欠下的一些CF这场前5个题都极其一眼,然而F瞪了好久愣是屁都不会感觉现在水平有有点到瓶颈了,以前是Div2D写完卡现在是Div2E写完卡,但至少还是在进步的A.CoverinWater如果存在某个空地块的长度大于\(2\)则可以用两个块造出无限水,否则答案就是所有空......
  • Codeforces Round 881 (Div. 3)
    CodeforcesRound881(Div.3)A:ABCA.SashaandArrayColoring题意:求最大的着色成本(着色成本是指同一个颜色的最大值-最小值)思路:肯定不能是相同的,直接最大-最小就行#include<bits/stdc++.h>usingnamespacestd;inta[60];voidsolve(){intn;cin>>n;......
  • [Codeforces] CF1807E Interview
    题目翻译有\(n\)堆石头,其中\(n-1\)堆都只有重量为一克的石头,剩下一堆有则有一块有两克的石头和若干重量为一克的石头。你的任务是在\(30\)次询问内推理出那一堆有重量为两克的石头是第几堆。首先输入\(n\),接下来输入\(n\)个数\(a_1,a_2\dotsa_n\),其中\(a_i\)表示......
  • Codeforces Round 911 (Div. 2)
    CodeforcesRound911(Div.2)A-CoverinWaterintmain(){IOS;for(cin>>_;_;--_){cin>>n>>s+1;intans=0;boolf=0;for(inti=1,j=1;i<=n;i=++j)if(s[i]=='......