Codeforces Round #595 (Div. 3) 题解

前言

A

因为没有重复的数,我们只要将数据排序,比较两两之间有没有\(a_j - a_i == 1 (j > i)\) 的,有则输出 \(2\) , 无则输出 \(1\)

普及T1难度

Code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 107
using namespace std;
inline int read() {
    int x=0,f=1; char ch=getchar();
    while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
    return x * f;
}
int q,n,ans;
int a[N];
void work() {
    memset(a, 0, sizeof(a));
    n = read();
    for(int i=1;i<=n;++i)
        a[i] = read();
    sort(a+1, a+1+n);
    bool flag = 1;
    for(int i=1;i<=n-1;++i) {
        if(a[i+1]-a[i] == 1) flag = 0;
    }
    if(flag) puts("1");
    else puts("2");
}
int main()
{
    q = read();
    while(q--) {
        work();
    }
    return 0;
}

B

通过模拟样例我们可以发现序列上有一些环 (这个很好理解吧?拿出纸笔画画qwq)

先预处理一下每个环的长度,和每个点所在环

然后直接输出即可

普及T2?

Code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
inline int read() {
    int x=0,f=1; char ch=getchar();
    while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
    return x * f;
}
const int N = 2e5+7;
int n,q,tot;
int p[N],color[N],cnt[N];
void work() {
    memset(color, 0, sizeof(color));
    tot = 0;
    n = read();
    for(int i=1;i<=n;++i)
        p[i] = read();
    for(int i=1,j,sum;i<=n;++i) {
        if(!color[i]) {
            color[i] = ++tot;
            j = p[i], sum = 1;
            while(j != i) j = p[j], ++sum, color[j] = tot;
            cnt[tot] = sum;
        }
    }
    for(int i=1;i<=n;++i)
        printf("%d ",cnt[color[i]]);
    putchar('\n');
}
int main()
{
    q = read();
    while(q--) work();
    return 0;
}

C

先把十进制转换为三进制

如 \(17\) -> \(1\) \(2\) \(2\)

从左往右数发现第二位有一个 "\(2\)",

再找 "2" 的前面出现的第一个 "\(0\)" ,把它改为 "\(1\)",如果前面没有 "\(0\)" 了,我们就加一位 (->\(1\) \(1\) \(2\) \(2\)) (第四位是改动的位置)

再从改动的位置向下把它下面的每一位改为 "\(0\)" (->\(1\) \(0\) \(0\) \(0\)) ,这个数我们就找到了

想一想,这样做为什么是对的?

∵ 我们要消除 "\(2\)" ∴ 最高位上的 "\(2\)" 是一定要消去的

∴ 找到比这个 "\(2\)" 高的最近的一位 "0" ,把它改为 "\(1\)" ,相当于进了一位,故把后面所有的数改为 "\(0\)"

提高 Day1T1 难度?

Code

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#define int long long
using namespace std;
inline int read() {
    int x=0,f=1; char ch=getchar();
    while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
    return x * f;
}
int n,q;
void work() {
    vector<int> num;
    n = read(); int t = n;
    while(n) num.push_back(n%3), n/=3;
    int x = -1LL;
    for(int i=num.size()-1;i>=0;--i)
        if(num[i] == 2) {
            x = i; break;
        }
    if(x == -1LL) printf("%lld\n",t);
    else {
        num.push_back(0);
        for(int i=x+1;i<num.size();++i)
            if(!num[i]) {
                num[i] = 1;
                for(int j=i-1;j>=0;--j) num[j] = 0LL;
                break;
            }
        x = 0LL;
        for(int i=num.size()-1;i>=0;--i) x = (x*3) + num[i];
        printf("%lld\n",x);
    }
}
signed main()
{
    q = read();
    while(q--) work();
    return 0;
}

D

先挖坑,慢慢补

E

F

原文地址:https://www.cnblogs.com/BaseAI/p/11723855.html

时间: 2024-08-30 11:16:47

Codeforces Round #595 (Div. 3) 题解的相关文章

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When

Codeforces Round #FF (Div. 2) 题解

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n 

Codeforces Round #259 (Div. 2) 题解

A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n?>?1) is an n?×?n 

Codeforces Round #177 (Div. 2) 题解

[前言]咦?现在怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛,一般我报一个数字A,随便再拉一个人选一个数字B.然后开始做第A^B场.如果觉得机密性不高,来点取模吧.然后今天做的这场少有的AK了.(其实模拟赛只做完了4题,最后1题来不及打了) 等等,话说前面几题不用写题解了?算了,让我难得风光一下啦. [A] A. Polo the Penguin and Segments time limit per test 2 seconds mem

Codeforces Round #534 (Div. 2)题解

Codeforces Round #534 (Div. 2)题解 A. Splitting into digits 题目大意 将一个数字分成几部分,几部分求和既是原数,问如何分可以使得分出来的各个数之间的差值尽可能小 解题思路 将n分成n个1相加即可 AC代码 #include<cstring> #include<string> #include<iostream> #include<cstdio> using namespace std; int main

Codeforces Round #561 (Div. 2) 题解

Codeforces Round #561 (Div. 2) 题解 题目链接 A. Silent Classroom 水题. Code #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 105; int n; char s[N], t[N]; int main() { cin >> n; for(int i = 1; i <= n; i++) { scanf(&q

Codeforces Round #608 (Div. 2) 题解

目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 程序 D. Portals 题意 做法 程序 E. Common Number 题意 做法 程序 结束语 Codeforces Round #608 (Div. 2) 题解 前言 题目链接:仅仅只是为了方便以题目作为关键字能查找到我的题解而已(逃 Codeforces 1271A Codeforce

Codeforces Round #617 (Div. 3) 题解

目录 Codeforces Round #617 (Div. 3) 题解 前言 A. Array with Odd Sum 题意 做法 程序 B. Food Buying 题意 做法 程序 C. Yet Another Walking Robot 题意 做法 程序 D. Fight with Monsters 题意 做法 程序 E1. String Coloring (easy version) 题意 做法 程序 E2. String Coloring (hard version) 题意 做法

[Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F

[Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F 1296A - Array with Odd Sum 思路: 如果一开始数组的sum和是奇数,那么直接YES, 否则:如果存在一个奇数和一个偶数,答案为YES,否则为NO 代码: int n; int a[maxn]; int main() { //freopen("D:\\code\\text\\input.txt","r",stdin); //freopen(