Codeforces Global Round 15(A~D)

Codeforces Global Round 15(https://codeforces.com/contest/1552

A. Subsequence Permutation(题目链接:https://codeforces.com/contest/1552/problem/A)
A string s of length n, consisting of lowercase letters of the English alphabet, is given.

You must choose some number k between 0 and n. Then, you select k characters of s and permute them however you want. In this process, the positions of the other n−k characters remain unchanged. You have to perform this operation exactly once.

For example, if s=“andrea”, you can choose the k=4 characters “a_d_ea” and permute them into “d_e_aa” so that after the operation the string becomes “dneraa”.

Determine the minimum k so that it is possible to sort s alphabetically (that is, after the operation its characters appear in alphabetical order).

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤40) — the length of the string.

The second line of each test case contains the string s. It is guaranteed that s contains only lowercase letters of the English alphabet.

Output
For each test case, output the minimum k that allows you to obtain a string sorted alphabetically, through the operation described above.

Example
inputCopy
4
3
lol
10
codeforces
5
aaaaa
4
dcba
outputCopy
2
6
0
4
Note
In the first test case, we can choose the k=2 characters “_ol” and rearrange them as “_lo” (so the resulting string is “llo”). It is not possible to sort the string choosing strictly less than 2 characters.

In the second test case, one possible way to sort s is to consider the k=6 characters “o__force” and rearrange them as “c__efoor” (so the resulting string is “ccdeefoors”). One can show that it is not possible to sort the string choosing strictly less than 6 characters.

In the third test case, string s is already sorted (so we can choose k=0 characters).

In the fourth test case, we can choose all k=4 characters “dcba” and reverse the whole string (so the resulting string is “abcd”).

题目大意:给定一个字符串s,选定整数k(0<=k<=n),然后从字符串中任意选取k个字符,按照任意你想要的方式排序,任务是找到一个最小的k
使得选取k个字符进行你想要的排序后整个字符串按照升序排列。
解法:将字符串备份排序,再比较排序过后有多少字符串不在原来的位置,
AC代码:

#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out                 \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++) 
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e5+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int solve()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    string t=s;
    int res=0;
    sort(t.begin(),t.end());
    for(int i=0;i<s.size();i++){
        if(s[i]!=t[i]) res++;
    }
    cout<<res;
    return 0;
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

B. Running for Gold(题目链接:https://codeforces.com/contest/1552/problem/B
The Olympic Games have just started and Federico is eager to watch the marathon race.

There will be n athletes, numbered from 1 to n, competing in the marathon, and all of them have taken part in 5 important marathons, numbered from 1 to 5, in the past. For each 1≤i≤n and 1≤j≤5, Federico remembers that athlete i ranked ri,j-th in marathon j (e.g., r2,4=3 means that athlete 2 was third in marathon 4).

Federico considers athlete x superior to athlete y if athlete x ranked better than athlete y in at least 3 past marathons, i.e., rx,j<ry,j for at least 3 distinct values of j.

Federico believes that an athlete is likely to get the gold medal at the Olympics if he is superior to all other athletes.

Find any athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes), or determine that there is no such athlete.

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of each test case contains a single integer n (1≤n≤50000) — the number of athletes.

Then n lines follow, each describing the ranking positions of one athlete.

The i-th of these lines contains the 5 integers ri,1,ri,2,ri,3,ri,4,ri,5 (1≤ri,j≤50000) — the ranking positions of athlete i in the past 5 marathons. It is guaranteed that, in each of the 5 past marathons, the n athletes have distinct ranking positions, i.e., for each 1≤j≤5, the n values r1,j,r2,j,…,rn,j are distinct.

It is guaranteed that the sum of n over all test cases does not exceed 50000.

Output
For each test case, print a single integer — the number of an athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes). If there are no such athletes, print −1. If there is more than such one athlete, print any of them.

Example
inputCopy
4
1
50000 1 50000 50000 50000
3
10 10 20 30 30
20 20 30 10 10
30 30 10 20 20
3
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
6
9 5 3 7 1
7 4 1 6 8
5 6 7 3 2
6 7 8 8 6
4 2 2 4 5
8 3 6 9 4
outputCopy
1
-1
1
5
Note
Explanation of the first test case: There is only one athlete, therefore he is superior to everyone else (since there is no one else), and thus he is likely to get the gold medal.

Explanation of the second test case: There are n=3 athletes.

Athlete 1 is superior to athlete 2. Indeed athlete 1 ranks better than athlete 2 in the marathons 1, 2 and 3.
Athlete 2 is superior to athlete 3. Indeed athlete 2 ranks better than athlete 3 in the marathons 1, 2, 4 and 5.
Athlete 3 is superior to athlete 1. Indeed athlete 3 ranks better than athlete 1 in the marathons 3, 4 and 5.
Explanation of the third test case: There are n=3 athletes.

Athlete 1 is superior to athletes 2 and 3. Since he is superior to all other athletes, he is likely to get the gold medal.
Athlete 2 is superior to athlete 3.
Athlete 3 is not superior to any other athlete.
Explanation of the fourth test case: There are n=6 athletes.

Athlete 1 is superior to athletes 3, 4, 6.
Athlete 2 is superior to athletes 1, 4, 6.
Athlete 3 is superior to athletes 2, 4, 6.
Athlete 4 is not superior to any other athlete.
Athlete 5 is superior to athletes 1, 2, 3, 4, 6. Since he is superior to all other athletes, he is likely to get the gold medal.
Athlete 6 is only superior to athlete 4.

题意:给定一个nx5的矩阵,分别表示n个运动员在五场比赛中的排名情况,若运动员A的五场比赛中有三场及以上排名高过
运动员B则A比B厉害,找出n个运动员中最厉害的人(冠军),如果找不到符合条件的人输出-1;

乍一看,感觉无从下手,但其实,我们不需要On2遍历所有的运动员,我们只需要一次遍历找出一个冠军候选人,最后验证一遍这个人是否是冠军,再输出结果。
遍历方法为,假设第一个是冠军,res=1,然后从第二个运动员开始比较,若第i个运动员的成绩优于res,则冠军候选res给到i(冠军的前提是要优于所有人,如果某运动员以及比其他人差他不可能再成为冠军,因此我们能保证这样得到的res是最有可能成为冠军的)
遍历结束后将res与剩下n-1一个人比较一遍确定res是否是冠军,不是则输出-1

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out                 \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++) 
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 5e4+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int a[N][5];
bool check(int fir,int sec){
    int cnt=0;
    for(int i=0;i<5;i++){
        if(a[fir][i]<a[sec][i]) cnt++;
    }
    return cnt>=3;
}
int solve()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<5;j++)
        cin>>a[i][j];
    }
    int res=0;
    for(int i=1;i<n;i++){
        if(check(i,res)) res=i;
    }
    //最后检查res是否优于所有人
    bool f=0;
    for(int i=0;i<n;i++){
        if(i!=res){
            if(check(i,res)){
                f=1;
                break;
            }
        }
    }
    if(f) cout<<-1;
    else cout<<res+1;
    return 0;
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

C. Maximize the Intersections(题目链接:https://codeforces.com/contest/1552/problem/C
On a circle lie 2n distinct points, with the following property: however you choose 3 chords that connect 3 disjoint pairs of points, no point strictly inside the circle belongs to all 3 chords. The points are numbered 1,2,…,2n in clockwise order.

Initially, k chords connect k pairs of points, in such a way that all the 2k endpoints of these chords are distinct.

You want to draw n−k additional chords that connect the remaining 2(n−k) points (each point must be an endpoint of exactly one chord).

In the end, let x be the total number of intersections among all n chords. Compute the maximum value that x can attain if you choose the n−k chords optimally.

Note that the exact position of the 2n points is not relevant, as long as the property stated in the first paragraph holds.

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

The first line of each test case contains two integers n and k (1≤n≤100, 0≤k≤n) — half the number of points and the number of chords initially drawn.

Then k lines follow. The i-th of them contains two integers xi and yi (1≤xi,yi≤2n, xi≠yi) — the endpoints of the i-th chord. It is guaranteed that the 2k numbers x1,y1,x2,y2,…,xk,yk are all distinct.

Output
For each test case, output the maximum number of intersections that can be obtained by drawing n−k additional chords.

Example
inputCopy
4
4 2
8 2
1 5
1 1
2 1
2 0
10 6
14 6
2 20
9 10
13 18
15 12
11 7
outputCopy
4
0
1
14
Note
In the first test case, there are three ways to draw the 2 additional chords, shown below (black chords are the ones initially drawn, while red chords are the new ones):
(图片懒得搬过来了,可以去cf上看)
We see that the third way gives the maximum number of intersections, namely 4.

In the second test case, there are no more chords to draw. Of course, with only one chord present there are no intersections.

In the third test case, we can make at most one intersection by drawing chords 1−3 and 2−4, as shown below:

题意:一个圆上有2n个点,顺时针排列,一开始给你k条弦,每条弦只能关联两个点,问你如何连剩下的点使最终n条弦交点最多,并给出交点数
数据很小,确定完哪些点应该连线后再遍历一遍统计出交点个数。
我们先不考虑是否已经有弦,假设给定n=4,需要4条弦来连接8个点使得交点数最多,结果是6,也就是说,我们把处于对称位置上的
点连接后能最大程度保证这些弦尽可能相交。有了上述结论,就可以愉快写代码了。
AC代码:

#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out                 \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++) 
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 5e4+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int a[305],b[305];
bool vis[305];
vector<int>lft;
int solve()
{
    reset(vis);
    reset(a);
    reset(b);
    lft.clear();
    int n,k;
    cin>>n>>k;
    if(k==0){
        cout<<n*(n-1)/2;
        return 0;
    }
    for(int i=0;i<k;i++){
        cin>>a[i]>>b[i];
        vis[a[i]]=vis[b[i]]=1;
        if(a[i]>b[i]) swap(a[i],b[i]);  //方便后面计数
    }
    for(int i=1;i<=2*n;i++)
        if(!vis[i]) lft.push_back(i);

    for(int i=0;i<lft.size()/2;i++){
        a[k]=lft[i];
        b[k++]=lft[i+lft.size()/2];
    }
    int res=0;
    for(int i=0;i<k;i++){
        for(int j=0;j<k;j++){
            if(a[i]<a[j]&&b[i]<b[j]&&a[j]<b[i]) res++;
        }
    }
    cout<<res;
    return 0;
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

D. Array Differentiation(题目链接:https://codeforces.com/contest/1552/problem/D)

You are given a sequence of n integers a1,a2,…,an.

Does there exist a sequence of n integers b1,b2,…,bn such that the following property holds?

For each 1≤i≤n, there exist two (not necessarily distinct) indices j and k (1≤j,k≤n) such that ai=bj−bk.
Input
The first line contains a single integer t (1≤t≤20) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤10).

The second line of each test case contains the n integers a1,…,an (−105≤ai≤105).

Output
For each test case, output a line containing YES if a sequence b1,…,bn satisfying the required property exists, and NO otherwise.

Example
inputCopy
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
outputCopy
YES
YES
NO
YES
YES
Note
In the first test case, the sequence b=[−9,2,1,3,−2] satisfies the property. Indeed, the following holds:

a1=4=2−(−2)=b2−b5;
a2=−7=−9−(−2)=b1−b5;
a3=−1=1−2=b3−b2;
a4=5=3−(−2)=b4−b5;
a5=10=1−(−9)=b3−b1.
In the second test case, it is sufficient to choose b=[0], since a1=0=0−0=b1−b1.

In the third test case, it is possible to show that no sequence b of length 3 satisfies the property.
题意:给定序列a,问是否存在和a长度相同的序列b,使得a中每一个数a[i]都等于b中某两个数相减,即a[i]=b[j]-b[k];1<=i,j,k<=n;
搜索序列a,找到是否有a的子序列的线性组合(系数是0,1,-1)和为0(至少两组,一组的系数是0,0…0)。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out                 \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++) 
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 5e4+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int res,a[15],n;
int dfs(int cnt,int sum){
    if(cnt==n-1){
        if(sum==0) res++;
        return 0;
    }
    dfs(cnt+1,sum+a[cnt+1]);
    dfs(cnt+1,sum-a[cnt+1]);
    dfs(cnt+1,sum);
    return 0;
}
int solve()
{
    cin>>n;
    forl(0,n) cin>>a[i];
    res=0;
    dfs(0,a[0]);
    dfs(0,-a[0]);
    dfs(0,0);
    if(res>=2) cout<<"YES";
    else cout<<"NO";
    // cout<<res<<endl;
    return 0; 
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值