当前位置:   article > 正文

Codeforces Round 967 (Div. 2) A~C

codeforces round 967 (div. 2)

本期封面来自黑神话:悟空 自己截的图
今天这场主要卡翻译,题面又臭又长,读得人很难受

A - Make All Equal

题意

给一个数组,每次可以选择相邻两个数(或是最后一个和第一个数),保证前一个数比后一个数小,然后随便删一个,问最少操作多少次能使数组都是同一个数

思路

这个限制其实就等于完全没有限制,把原本数量最多的那个数字留下即可

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int st[114];

void solve()
{
    int n;
    scanf("%d",&n);
    memset(st,0,sizeof(st));
    vector<int> a(n);
    int maxn=0;
    for (int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        st[a[i]]++;
        if(st[a[i]]>st[maxn])
        {
            maxn=a[i];
        }
    }
    int ans=n-st[maxn];
    printf("%d\n",ans);
}

int main()
{
    int T=1;
    scanf("%d",&T);
    while(T--)
    {
        solve();
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

B - Generate Permutation

题意

现在两个打字机,都只能按数字顺序打出字,就一个只能从左往右一个只能从右往左,构造一个长度为n的排列,让这两台打字机循环的次数最少

思路

构造题,偶数肯定不行输出-1,奇数就是左右交替

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

void solve()
{
    int n;
    scanf("%d",&n);
    if(n%2==0)
        printf("-1\n");
    else
    {
        vector<int> a(n);
        int cnt=1,l=0,r=n-1;
        while(l<r)
        {
            a[l]=cnt++;
            a[r]=cnt++;
            l++;
            r--;
        }
        a[l]=cnt++;
        for(int i=0;i<n;i++)
            printf("%d ",a[i]);
        printf("\n");
    }
}

int main()
{
    int T=1;
    scanf("%d",&T);
    while(T--)
    {
        solve();
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

C - Guess The Tree

题意

一个数有n个节点,在 15 n 15n 15n次询问之内只到这棵树的所有边,每次询问你给他两个点a和b,他会返回使 ∣ d ( a , x ) − d ( b , x ) ∣ |d(a,x) - d(b,x)| d(a,x)d(b,x)最小的那个x节点,如果有多个相同的会保证给的节点还最小化 d ( a , x ) d(a,x) d(a,x)

思路

主要是很难调,数据卡得很死,其实就是递归的一个过程,没问过的都问一遍就行了

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n;
int st[1114];
vector<pii> ans;
void ask(int x,int y)
{
    printf("? %d %d\n",x,y);
    fflush(stdout);
    int res;
    scanf("%d",&res);
    if(res==x or res==y)
    {
        ans.push_back({x,y});
        st[res]=1;
        st[y]=1;
        return ;
    }
    if(st[x] and st[res])
    {
        ask(res,y);
    }
    else
    {
        ask(x,res);
        ask(res,y);
    }
}

void solve()
{
    scanf("%d",&n);
    if(n==2)
    {
        printf("! 1 2\n");
        fflush(stdout);
        return ;
    }
    memset(st,0,sizeof(st));
    for(int i=1;i<n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(st[j]) continue;
            ask(i,j);
        }
    }
    printf("!");
    for(auto x:ans)
    {
        printf(" %d %d",x.first,x.second);
    }
    printf("\n");
    fflush(stdout);
    ans.clear();
}

int main()
{
    int T=1;
    scanf("%d",&T);
    while(T--)
    {
        solve();
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/1021091
推荐阅读
相关标签
  

闽ICP备14008679号