首页 > 其他分享 >Codeforces Round 874 (Div. 3)

Codeforces Round 874 (Div. 3)

时间:2023-05-29 18:01:08浏览次数:42  
标签:begin ch return int 874 Codeforces read Div getchar

A. Musical Puzzle

#include <bits/stdc++.h>

using namespace std;

void solve(){
    int n;
    string s;
    cin >> n >> s;
    set<string> cnt;
    for( int i = 0 ; i + 1 < n ; i ++ )
        cnt.insert( s.substr( i , 2 ) );
    cout << cnt.size() << "\n";
    return ;
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    while( t -- )
        solve();
    return 0;
}

B. Restore the Weather

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


void solve() {
    int n = read(), k = read();
    vector<pair<int, int>> a(n);
    vector<int> b(n), c(n);
    for (int i = 0; i < n; i++)
        a[i].first = read(), a[i].second = i;
    for (auto &i: b) i = read();



    sort(a.begin(), a.end()), sort(b.begin(), b.end());
    for( int i = 0 ; i < n ; i ++ ) c[ a[i].second ] = b[i];
    for( auto i : c )
        printf("%d " , i );
    printf("\n");
    return;
}

int32_t main() {
    for (int t = read(); t; t--)
        solve();
    return 0;
}

C. Vlad Building Beautiful Array

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


void solve() {
    int n = read();
    vector<int> a(n);
    int b = INT_MAX , c = INT_MAX;
    for( auto & i : a ) {
        i = read();
        if( i & 1 ) b = min( b , i );
        else c = min( c , i );
    }
    if( b == INT_MAX || c == INT_MAX ){
        cout << "YES\n";
        return ;
    }
    int f = 1;
    for( const auto & i : a ){
        if( i&1 ) continue;
        if( b < i ) continue;
        f = 0;
        break;
    }
    if( f ) cout << "YES\n";
    else cout << "NO\n";
        return;
}

int32_t main() {
    for (int t = read(); t; t--)
        solve();
    return 0;
}

D. Flipper

因为要必须翻转一次,所以为了字典序最大,所以一定要把最大值翻到前面,所以右端点一定是\(n\),但是当\(n\)在第一个位置上是,因为必须翻转,所以有段点的值就是\(n-1\)

当确定右端点后,可以直接枚举左端点的位置,然后暴力翻转,因为\(n\)很小所以复杂度可以接受。

这里学到的一个新的函数std::rotate环移函数,

std::rotate(iterator begin, iterator middle, iterator end);

作用是把序列中beginend连起来,然后再从middle处断开,middle作为新的begin

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


void solve() {
    int n = read();
    vector<int> a(n);
    for( auto & i : a ) i = read();
    vector res( a.rbegin(), a.rend());
    int r = find( a.begin(), a.end() , n ) - a.begin();
    if( r == 0 && n > 1 ) r = find( a.begin(),a.end() , n-1 ) - a.begin();
    for( int i = 0 ; i < n ; i ++ ){
        auto b = a;
        reverse(b.begin()+i , b.end() );
        rotate( b.begin() , b.begin()+i ,b.end() );
        res = max( res , b );
    }
    for( int i = 0 ; i < r ; i ++ ){
        auto b = a ;
        reverse( b.begin()+i , b.begin() + r );
        rotate( b.begin() , b.begin()+i , b.end() );
        rotate( b.begin() , b.begin()+r-i , b.end() - i );
        res = max( res , b) ;
    }
    for( auto i : res )
        printf("%d " , i );
    printf("\n");

    return;
}

int32_t main() {
    for (int t = read(); t; t--)
        solve();
    return 0;
}
 

E. Round Dance

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


vector<vector<int>> e;
vector<int> vis;

void dfs(int u, int fa) {
    if (vis[u] == 1) {
        vis[u] = 2;
        return;
    }
    vis[u] = 1;
    for (auto v: e[u]) {
        if (v == fa) continue;
        dfs(v, u);
    }
    return;
}

void solve() {
    int n = read();
    e = vector<vector<int>>(n + 1);
    vis = vector<int>(n + 1, 0);
    int a = 0, b = 0;
    for (int i = 1, x; i <= n; i++)
        x = read(), e[i].push_back(x), e[x].push_back(i);
    for (auto &i: e)
        sort(i.begin(), i.end()), i.resize(unique(i.begin(), i.end()) - i.begin());
    for (int i = 1; i <= n; i++) {
        if (vis[i]) continue;
        dfs(i, -1);
        if (vis[i] == 1) a++;
        else b++;
    }
    printf("%d %d\n", (b + (a > 0)), a + b);
    return;
}

int32_t main() {
    for (int t = read(); t; t--)
        solve();
    return 0;
}

F. Ira and Flamenco

#include <bits/stdc++.h>

using namespace std;

#define int long long

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 mod = 1e9 + 7;

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

int inv(int x) {
    return power(x, mod - 2);
}

void solve() {
//    printf("\n\n\n");
    int n = read(), m = read();
    map<int, int> cnt;
    vector<int> a, b;
    for (int i = 1, x; i <= n; i++) x = read(), cnt[x]++;
    for (auto [k, v]: cnt) a.push_back(k), b.push_back(v);
    int res = 0, ans = 1;
    for (int i = 0; i < m-1 && i < a.size(); i++)
        ans = ans * b[i] % mod;
    for (int i = m - 1; i < a.size(); i++) {
        ans = ans * b[i] % mod;
        if (a[i] - a[i - m + 1] == m-1) res = (res + ans) % mod;
        ans = ans * inv(b[i - m+1]) % mod;
    }
    printf("%lld\n", res);
    return;
}

int32_t main() {
    for (int t = read(); t; t--)
        solve();
    return 0;
}

G. Ksyusha and Chinchilla

在 dfs的过程中,每次发现子树大小等于3 时就把与这条子树相连的边删掉。

#include <bits/stdc++.h>

using namespace std;

#define int long long

typedef pair<int, int> edge;

vector<vector<edge>> e;
vector<int> sz , res;

bool dfs( int x , int fa ){
    sz[x] = 1;
    for( auto [y,i] : e[x] ){
        if( y == fa ) continue;
        if( !dfs( y , x ) ) return false;
        if( sz[y] == 3 ) res.push_back(i);
        else sz[x] += sz[y];
    }
    return sz[x] <= 3;
}

void solve() {
    int n;
    cin >> n;
    e = vector<vector<edge>>(n + 1);
    sz = vector<int>(n + 1 , 0);
    res = vector<int>();
    for (int i = 1, x, y; i < n; i++)
        cin >> x >> y, e[x].emplace_back(y, i), e[y].emplace_back(x, i);

    if( n % 3 || !dfs( 1 , -1 ) || sz[1] != 3 ) {
        cout << "-1\n";
        return;
    }
    cout << res.size() << "\n";
    for( auto i : res ) cout << i << " ";
    cout << "\n";
    return;
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    for (; t; t--)
        solve();
    return 0;
}

标签:begin,ch,return,int,874,Codeforces,read,Div,getchar
From: https://www.cnblogs.com/PHarr/p/17441234.html

相关文章

  • Codeforces Round 875 (Div. 2) A-D
    CodeforcesRound875(Div.2)A.TwinPermutationsinta[N];voidsolve(){intn=read();for(inti=1;i<=n;i++)a[i]=read();for(inti=1;i<=n;i++)cout<<n+1-a[i]<<'';cout<<'\n';//puts(ans&g......
  • CodeForces 1830C Hyperregular Bracket Strings
    洛谷传送门CF传送门每一步思路都非常自然的题。考虑先从一些简单的case入手。1.\(k=0\)设\(g_i\)为长度为\(i\)的合法括号串数。显然\(\foralli\nmid2,g_i=0\)。对于偶数的\(i\),这是一个经典问题,\(g_i\)就是第\(\frac{i}{2}\)项卡特兰数,因此\(g_i=\bi......
  • Codeforces Round #875 (Div. 2)
    CodeforcesRound#875(Div.2)bilibili:CodeforcesRound#875(Div.2)实况|完成度[4.01/6]A#include<bits/stdc++.h>usingll=longlong;usinguint=unsigned;usingnamespacestd;intTestCase;signedmain(){ for(scanf("%d",&......
  • Codeforces Round 875 (Div. 2) A~D
    CodeforcesRound875(Div.2)A~DA.TwinPermutations构造\(a[i]+b[i]=n+1\)voidwork(){ intn; cin>>n; rep(i,1,n){ intx; cin>>x; cout<<n-x+1<<""; } cout<<endl;}B.Arraymerging......
  • HTML中让上下两个div之间保持一定距离或没有距离
    这篇文章主要为大家详细介绍了HTML让上下两个DIV之间保持一定距离或没有距离,可以用来参考一下。1、若想上下DIV块之间距离,只需设定:在CSS里设置DIV标签各属性参数为0div{margin:0;border:0;padding:0;}这里就设置了DIV标签CSS属性相当于初始化了DIV标签CSS属性,这里设置margin外......
  • Educational Codeforces Round 149 (Rated for Div. 2)(A~F)
    A.GrasshopperonaLine题意:给出n,k,从0开始,每次可以加一个数,最快到达n需要,输出首先跳几次,然后每次跳多少,限制只有一个跳的长度不能整除k。分析:n%k,有余直接跳,没余数,先跳一个,再跳剩余的长度。代码:#include<cstring>#include<iostream>#include<algorithm>usingnamespac......
  • 29. Divide Two Integers刷题笔记
    参考的题解classSolution:defdivide(self,dividend:int,divisor:int)->int:positive=(dividend<0)is(divisor<0)dividend,divisor=abs(dividend),abs(divisor)res=0whiledividend>=divisor:......
  • Codeforces 1444E - Finding the Vertex
    非常神秘的一道题,当之无愧的*3500。首先考虑转化题意。考虑一种决策树,由于我们每次问一条边之后,相当于会根据信息删掉两个连通块中的一个,因此一种决策树实际上对应了原树的一棵边分树。而为了让最坏情况下的询问次数最少,我们目标实际上是最小化边分树的深度。考虑借鉴P5912JA......
  • Educational Codeforces Round 149 (Rated for Div. 2) 题解
    https://codeforces.com/contest/1837https://codeforces.com/contest/1837/problems利益相关:上紫祭。真的不要以为这道题放在F就不敢做。压线过题的感觉真好。ABC题都过水,就不写了。代码丢在这里:A:https://codeforces.com/contest/1837/submission/207156920B:https:......
  • 用fetch处理的流,放到div中会有样式丢失的问题
    最近在做fetch处理流的问题,发现接收到得流,在div中渲染得时候样式会丢失,特别是空格、换行之类得。为了解决问题去看看了css得样式处理发现css中有个属性white-space。然后就去看了这个属性的定义和取值情况。   在css中white-space属性用来控制容器的文本中带有空白符、制表......