2020-11-14 codeforces总结(A-C题)

总结:

一定要看题目给的数的范围

A. Special Permutation

You are given one integer n (n>1).

Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation of length 5, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).

Your task is to find a permutation p of length n that there is no index i (1≤i≤n) such that pi=i (so, for all i from 1 to n the condition pi≠i should be satisfied).

You have to answer t independent test cases.

If there are several answers, you can print any. It can be proven that the answer exists for each n>1.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.

The only line of the test case contains one integer n (2≤n≤100) — the length of the permutation you have to find.

Output
For each test case, print n distinct integers p1,p2,…,pn — a permutation that there is no index i (1≤i≤n) such that pi=i (so, for all i from 1 to n the condition pi≠i should be satisfied).

If there are several answers, you can print any. It can be proven that the answer exists for each n>1.

Example
inputCopy
2
2
5
outputCopy
2 1
2 1 5 3 4

大意:
给你一个数,输出一个数组,要求ai!=i;


错了一次。。。刚开始的时候想着倒序输出就行了(忽略了奇数的情况)


对啊。。。。先从2输出,最后输出1也行啊啊啊。。。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        if(n%2==0)
            for(int i=n;i>=1;i--)
            cout<<i<<" ";
        else
 
        {
            for(int i=n;i>n/2+1;i--)
            cout<<i<<" ";
            for(int i=1;i<=n/2+1;i++)
                cout<<i<<" ";
        }
 
        cout<<endl;
    }
    return 0;
}

官方题解:
在这里插入图片描述

B. Unique Bid Auction

There is a game called “Unique Bid Auction”. You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don’t have to do it to solve this problem).

Let’s simplify this game a bit. Formally, there are n participants, the i-th participant chose the number ai. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of a the minimum one is the winning one).

Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is 1-based, i. e. the participants are numbered from 1 to n.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of participants. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th participant chosen number.

It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.

Example
inputCopy
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
outputCopy
-1
2
4
1
2
-1

题目大意:给出一组数,找出这组数中唯一存在的最小的那个,否则输出-1;

刚开始过了,后来被hack了,没看到题目里ai的取值范围;循环直接来了个200000;

#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    int da[200001];
    int id[200001];
    while(t--)
    {
        memset(da,0,sizeof(da));
        int n;
        cin>>n;
        for(int i=1; i<=n; i++)
        {
            int x;
            cin>>x;
            da[x]++;
            id[x]=i;
        }
        int k=-1;
        for(int i=0; i<=n; i++)
        {
            if(da[i]==1)
            {
                k=id[i];
                break;
            }
        }
        cout<<k<<endl;
    }
    return 0;
}

C. Sequence Transformation

You are given a sequence a, initially consisting of n integers.

You want to transform this sequence so that all elements in it are equal (i. e. it contains several occurrences of the same element).

To achieve this, you choose some integer x that occurs at least once in a, and then perform the following operation any number of times (possibly zero): choose some segment [l,r] of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains x. More formally, you choose some contiguous subsequence [al,al+1,…,ar] such that ai≠x if l≤i≤r, and remove it. After removal, the numbering of elements to the right of the removed segment changes: the element that was the (r+1)-th is now l-th, the element that was (r+2)-th is now (l+1)-th, and so on (i. e. the remaining sequence just collapses).

Note that you can not change x after you chose it.

For example, suppose n=6, a=[1,3,2,4,1,2]. Then one of the ways to transform it in two operations is to choose x=1, then:

choose l=2, r=4, so the resulting sequence is a=[1,1,2];
choose l=3, r=3, so the resulting sequence is a=[1,1].
Note that choosing x is not an operation. Also, note that you can not remove any occurrence of x.

Your task is to find the minimum number of operations required to transform the sequence in a way described above.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th element of a.

It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer — the minimum number of operations required to transform the given sequence in a way described in the problem statement. It can be proven that it is always possible to perform a finite sequence of operations so the sequence is transformed in the required way.

Example
inputCopy
5
3
1 1 1
5
1 2 3 4 5
5
1 2 3 2 1
7
1 2 3 1 2 3 1
11
2 2 1 2 3 2 1 2 3 1 2
outputCopy
0
1
1
2
3
题意:给出一个数组,你可以确定一个x,然后选一个范围(不能有x),去掉这些元素,最后只剩x,问最小的操作数;
思路没错,一直在超时。。。一定要记着看给出数值的范围啊啊。。


思路:先把相邻切重复的元素去掉(只留一个),然后计数,把第一个删去,最后一个删去;别忘了加一;

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    int a[200001];
    int b[200001];
    while(t--)
    {
        memset(b,0,sizeof(b));
        int n;
        scanf("%d",&n);
        int top=0;
        for(int i=0; i<n; i++)//输入加去重
        {
            int x;
            scanf("%d",&x);
            if(top==0)
                a[top++]=x;
            else
            {
                if(a[top-1]!=x)
                    a[top++]=x;
            }
        }
        for(int i=0; i<top; i++)
        {
            b[a[i]]++;
        }
        b[a[top-1]]--;//第二个减去
        b[a[0]]--;//第一个减去
        int minn=n+1;//找到最小的,别用sort(时间复杂度不如遍历)
        for(int i=0; i<top; i++)
        {
            if(minn>b[a[i]])
                minn=b[a[i]];
        }
        printf("%d\n",minn+1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JdiLfc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值