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

Codeforces Round 867 (Div. 3)

时间:2023-04-26 20:26:49浏览次数:43  
标签:cnt ch int Codeforces 867 while && Div getchar

A. TubeTube Feed

#include <bits/stdc++.h>

using namespace std;


int main() {
    int q;
    cin >> q;
    while (q--) {
        int n, t, res = -1, id = -1;
        cin >> n >> t;
        vector<int> a(n + 1), b(n + 1);
        for (int i = 1; i <= n; i++) cin >> a[i];
        for (int i = 1; i <= n; i++) cin >> b[i];
        for (int i = 1; i <= n; i++) {
            if (a[i] <= t) {
                if (b[i] > res) res = b[i], id = i;
            }
            t--;
        }
        cout << id << "\n";
    }
}

B. Karina and Array

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

void solve(){
	int n = read();
	vector<int> a(n);
	for( auto & i : a ) i = read();
	sort( a.begin() ,a.end() );
	printf("%lld\n" , max( a[0] * a[1] , a[n-1] * a[n-2] ) );
}

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

C. Bun Lover

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



int32_t main(){
	for( int t = read() , n ; t ; t -- ){
		n = read();
		printf("%lld\n" , (n+1)*(n+1) + 1 );
	}
}

D. Super-Permutation

打表找规律,发现\(b_i+1\)一定有\(n,1,n-1,2,n-2,3,\cdots\)这样的,根据\(b_i\)还原\(a_i\)即可

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



int32_t main(){
	for( int t = read() , n ; t ;  t -- ){
		n = read();
		if( n == 1 ){
			printf("1\n");
		}else if( n & 1 ){
			printf("-1\n");
		}else{
			int l = 1 , r = n;
			vector<int> a;
			for( int i = n / 2 ; i ; i -- , l ++ , r -- )
				a.push_back(l-1) , a.push_back(r-1);
			printf("%d" , n );
			for( int i = 1 , s = n , x  ; i < n ; i ++ ){
				x = ((a[i] - s) % n  + n) % n;
				s = ( s + x ) % n;
				printf(" %d" , x );
			}
			printf("\n");
		}
	}
}

E. Making Anti-Palindromes

如果有任意个字母出现次数超过一半,则一定无法构成。

统计每一个相同的字母对的数量。如果有一种字母的对数大于\(\frac n 2\),则一定不可以。

交换次数,要么是最多的字母对数,要么是重复的最多的对数。两者取较大值即可。

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

#define Count(arr) ( accumulate(arr.begin(),arr.end(),0ll) )
#define Max(arr) ( *max_element( arr.begin() , arr.end() ) )

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    for (int n, m; t; t--) {
        cin >> n;
        string s;
        cin >> s;
        if (n & 1) {
            cout << "-1\n";
            continue;
        }
        m = n / 2;
        vector<int> cnt(26), p(26);
        for (int i = 1; i <= m; i++) {
            if (s[i - 1] == s[n - i]) cnt[s[i - 1] - 'a']++;
            p[s[i - 1] - 'a']++, p[s[n - i] - 'a']++;
        }
        if (Max(p) > m) cout << "-1\n";
        else cout << max(Max(cnt), (Count(cnt) + 1) / 2) << "\n";
    }
}

F. Gardening Friends

首先找到直径的的两个端点\(p,q\),树上任意一个点距离最远的点一定包括\(p,q\)中至少一个。所以计算出任意点的\(p,q\)的距离,就可以\(O(1)\)的计算出根移动到每一个的点的收益。枚举根移动的位置取最大收益即可。

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

void solve() {
    int n = read(), k = read(), c = read();
    vector<vector<int>> e(n + 1);
    for (int i = n - 1, x, y; i; i--)
        x = read(), y = read(), e[x].push_back(y), e[y].push_back(x);
    auto bfs = [e, n](int x) {
        vector<int> d(n+1, -1);
        queue<int> q;
        q.push(x), d[x] = 0;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (auto v: e[u]) {
                if (d[v] >= 0) continue;
                d[v] = d[u] + 1, q.push(v);
            }
        }
        return d;
    };

    auto d1 = bfs(1);
    int p = max_element(d1.begin(), d1.end()) - d1.begin();
    auto d2 = bfs(p);
    int q = max_element(d2.begin(), d2.end()) - d2.begin();
    auto d3 = bfs(q);
    int res = 0;
    for (int i = 1; i <= n; i++)
        res = max(res, max(d2[i], d3[i]) * k - d1[i] * c);
    printf("%lld\n", res);
    return;
}

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

G1. Magic Triples (Easy Version)

对于每一个组数,\((a_i,a_j,a_k)\)如果枚举 出\(a_i\),实际上就是\(a_i,a_ix,a_ix^2\),这里\(x\)的取值范围是\(10^3\),实际上远远不到这么多。

#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 N = 1e6+5;
int cnt[N];

void solve() {
    int n = read() , res = 0;
    set<int> st;
    for( int i = n , x ; i ; i -- )
        x = read() , cnt[x] ++ , st.insert(x);
    for( auto i : st ){
        if( cnt[i] == 0 ) continue;
        if( cnt[i] >= 3 ) res += cnt[i] * (cnt[i]-1) * (cnt[i]-2);
        for( int j = 2 ; j * j * i <= *st.rbegin() ; j ++ ){
            res += cnt[i] * cnt[i*j] * cnt[i*j*j];
        }
    }
    for( auto i : st ) cnt[i] = 0;
    printf("%lld\n" , res);
    return;
}

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

G2. Magic Triples (Hard Version)

考虑改变枚举方式,枚举\(a_j\),则三元组就是\((\frac {a_j} b,a_j,a_jb)\),考虑到\(b\)一定是\(a_j\)的因子,所以我们可以算出\(a_j\)的所有因子,但是计算因子是\(\sqrt {a_j}\)的。

我们考虑按照值域分治,当\(a_j<S\)时枚举因子,复杂度\(O(\sqrt S)\),当\(a_j\ge S\)时,采取暴力枚举,复杂度\(O(\frac{10^9}{S})\),计算可知当\(S=10^6\)时,两种情况的复杂度都是\(O(10^3)\)

#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 S = 1e6;

vector<int> getFac(int x) {
    vector<int> f;
    for (int i = 1, t = sqrt(x); i <= t; i++){
        if( x % i ) continue;
        f.push_back(i);
        if( i * i != x ) f.push_back(x/i);
    }
    return f;
}

void solve() {
    int n = read(), res = 0;
    map<int, int> cnt;
    for (int i = 1, x; i <= n; i++)
        x = read(), cnt[x]++;
    for (auto [x, y]: cnt) {
        res += y * (y - 1) * (y - 2);
        if (x >= S) {
            for( int i = 2 ; i * x <= cnt.rbegin()->first ; i ++ )
                if( x % i == 0 && cnt.count(x/i) && cnt.count(x*i) )
                    res += cnt[x/i] * cnt[x*i] * y;
        } else {
            auto f = getFac(x);
            for( auto i : f ){
                if( i == 1 ) continue;
                if( i * x > cnt.rbegin()->first) continue;
                if( cnt.count(x/i) && cnt.count(x*i) )
                    res += cnt[x/i] * cnt[x*i] * y;
            }
        }
    }
    cout << res << "\n";
    return;
}

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

标签:cnt,ch,int,Codeforces,867,while,&&,Div,getchar
From: https://www.cnblogs.com/PHarr/p/17357141.html

相关文章

  • Educational Codeforces Round 147 (Rated for Div. 2)
    Preface补题这周比赛挺少,不过后面可能就很少有时间补以前的比赛了的说现在除了要做学校的集训专题外,还要刷一下蓝桥杯国赛的题,可以说是很忙碌了而且由于JAVA的期末考试要来了,为了熟悉下语法以后补题的时候前面的题目可能会用JAVA来写(其实我写的JAVA看起来和C++真没啥区别)A.......
  • B. Equalize by Divide - 贪心+思维+构造+数学+排序
    题意:给定一个数组,可以进行任意多次以下操作:1.选择第i和第j个数。2.使a[i]=a[i]/a[j](向上取整)。不可以插入或者删减数组元素,求多少次使数组元素都相同,输出次数以及每次操作的两个下标i,j;如果无法实现输出-1.分析:数组中存在1一定无法实现,或者数组元素都相......
  • Codeforces 1781G - Diverse Coloring(构造)
    vp时候想到大致思路了,但是死活调不对,赛后套取cf的数据调了好久才过/ll首先直觉告诉我们答案不会太大。稍微猜一猜可以猜出除了四个点的菊花之外答案都是\(n\bmod2\),下面我们来通过构造证明这件事情。首先,链的情况是trivial的,直接根据奇偶性间隔染色即可。如果不是链,那么......
  • Educational Codeforces Round 147(A~E)(补提记录)
    EducationalCodeforcesRound147(RatedforDiv.2)A:题意:每个问号都能被替换成0~9,求替换每个问号后所能的到的数的数量注:所得到的序列不能有前导0思路:先判断第一位是什么,作为特判,为0,则不能得到任何数输出0,为?则答案×9再依次枚举之后的每一个数,若为问号答案*10#include<io......
  • Codeforces Round 867 (Div. 3)(A-C)
    A.TubeTubeFeed签到题思路往后走,每次减一,记录当前所能得到最大的bi完整代码#include<bits/stdc++.h>usingnamespacestd;#definelllonglonginlinellread(){lls=0;charg=getchar();while(g>'9'||g<'0')g=getchar();while(g&g......
  • CF1479 Div1 VP记录
    战况:别的不说,这个B1WA3发是真的精髓。A略B我们设此时在第一队队尾的为las0,在第二队队尾的为las1,要放的数为x。先考虑B1:显然有:如果las0等于x,放在第二队,如果las1等于x,放在第一队。考虑两边都不同的情况,我们想要这个x后面尽快跟上一个不同的数,依此来创造新的......
  • Codeforces Round #459 (Div. 2) D. MADMAX DAG&&博弈
    Asweallknow,Maxisthebestvideogameplayeramongherfriends.Herfriendsweresojealousofhers,thattheycreatedanactualgamejusttoprovethatshe’snotthebestatgames.Thegameisplayedonadirectedacyclicgraph(aDAG)withnvertic......
  • 2014 Pacific Northwest Region Programming Contest—Division 2 Problem U — lim
    Incollegefootball,manydifferentsourcescreatealistoftheTop25teamsinthecountry.Sinceit’ssubjective,theselistsoftendiffer,butthey’reusuallyverysimilar.Yourjobistocomparetwooftheselists,anddeterminewheretheyaresimi......
  • Codeforces Round #306 (Div. 2) D. Regular Bridge 构造
    Anundirectedgraphiscalledk-regular,ifthedegreesofallitsverticesareequalk.Anedgeofaconnectedgraphiscalledabridge,ifafterremovingitthegraphisbeingsplitintotwoconnectedcomponents.Buildaconnectedundirectedk-regularg......
  • Codeforces Round #465 (Div. 2) D. Fafa and Ancient Alphabet 数学概率
    AncientEgyptiansareknowntohaveusedalargesetofsymbolstowriteonthewallsofthetemples.FafaandFifawenttooneofthetemplesandfoundtwonon-emptywordsS1andS2ofequallengthsonthewalloftemplewrittenonebelowtheother.Sinc......