CodeCraft-20 (Div. 2) A、B、C

A. Grade Allocation
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
n students are taking an exam. The highest possible score at this exam is m. Let ai be the score of the i-th student. You have access to the school database which stores the results of all students.

You can change each student’s score as long as the following conditions are satisfied:

All scores are integers
0≤ai≤m
The average score of the class doesn’t change.
You are student 1 and you would like to maximize your own score.

Find the highest possible score you can assign to yourself such that all conditions are satisfied.

Input
Each test contains multiple test cases.

The first line contains the number of test cases t (1≤t≤200). The description of the test cases follows.

The first line of each test case contains two integers n and m (1≤n≤103, 1≤m≤105) — the number of students and the highest possible score respectively.

The second line of each testcase contains n integers a1,a2,…,an (0≤ai≤m) — scores of the students.

Output
For each testcase, output one integer — the highest possible score you can assign to yourself such that both conditions are satisfied._

Example
inputCopy
2
4 10
1 2 3 4
4 5
1 2 3 4
outputCopy
10
5
Note
In the first case, a=[1,2,3,4], with average of 2.5. You can change array a to [10,0,0,0]. Average remains 2.5, and all conditions are satisfied.

In the second case, 0≤ai≤5. You can change a to [5,1,1,3]. You cannot increase a1 further as it will violate condition 0≤ai≤m.

题意:给出n个学生的成绩 给出成绩的上限m 最大化其中某一个学生的成绩 并且使得平均分不变 且其余学生的成绩大于等于0
思路:比较成绩的总和 和 m的大小即可 输出小的那个

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second

int a[1005];

int main()
{
    int t;

    cin >> t;

    while(t --)
    {
        int n,m;
        cin >> n >> m;

        int sum = 0;

        for(int i = 0;i < n;i ++)
            cin >> a[i],sum += a[i];

        if(sum > m)
            cout << m << '\n';
        else
            cout << sum << '\n';
    }
    return 0;
}

B. String Modification
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya has a string s of length n. He decides to make the following modification to the string:

Pick an integer k, (1≤k≤n).
For i from 1 to n−k+1, reverse the substring s[i:i+k−1] of s. For example, if string s is qwer and k=2, below is the series of transformations the string goes through:
qwer (original string)
wqer (after reversing the first substring of length 2)
weqr (after reversing the second substring of length 2)
werq (after reversing the last substring of length 2)
Hence, the resulting string after modifying s with k=2 is werq.
Vasya wants to choose a k such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of k. Among all such k, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
Each test contains multiple test cases.

The first line contains the number of test cases t (1≤t≤5000). The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤5000) — the length of the string s.

The second line of each test case contains the string s of n lowercase latin letters.

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

Output
For each testcase output two lines:

In the first line output the lexicographically smallest string s′ achievable after the above-mentioned modification.

In the second line output the appropriate value of k (1≤k≤n) that you chose for performing the modification. If there are multiple values of k that give the lexicographically smallest string, output the smallest value of k among them.

Example
inputCopy
6
4
abab
6
qwerty
5
aaaaa
6
alaska
9
lfpbavjsm
1
p
outputCopy
abab
1
ertyqw
3
aaaaa
1
aksala
6
avjsmbpfl
5
p
1
Note
In the first testcase of the first sample, the string modification results for the sample abab are as follows :

for k=1 : abab
for k=2 : baba
for k=3 : abab
for k=4 : baba
The lexicographically smallest string achievable through modification is abab for k=1 and 3. Smallest value of k needed to achieve is hence 1.

题意:字符串长度为n 每次选择1~n中的一个数k 使得字符串中的长度为k的子串翻转 使得字符串字典序最小 并且输出最小的k
思路:看数据范围直接暴力估计会T 观察样例可以发现规律 当字符串长度n和k的奇偶性相同时只要把长度为k的子串正序接到后面即可 反之逆序到后面 扔在一起排序即可

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second


struct node
{
    string res;
    int k;
}a[5005];

bool cmp(node a,node b)
{
    if(a.res == b.res)
        return a.k < b.k;
    return a.res < b.res;
}

int main()
{
    int T;

    cin >> T;

    while(T --)
    {
        int n;

        string s;

        cin >> n >> s;

        a[0].res = s,a[0].k = 1;

        for(int i = 1;i < n;i ++)
        {
            string t = "";

            for(int j = i;j < n;j ++)
                t += s[j];

            if((i+1)%2 == 0 && n%2 == 0 || (i+1)%2 && n%2)
            {
                for(int j = i-1;j >= 0;j --)
                {
                    t += s[j];
                }
            }
            else
            {
                for(int j = 0;j < i;j ++)
                {
                    t += s[j];
                }
            }

            a[i].res = t;
            a[i].k = i + 1;
        }

        sort(a,a + n,cmp);

        cout << a[0].res << '\n' << a[0].k << '\n';
    }
    return 0;
}

C. Primitive Primes
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
It is Professor R’s last class of his teaching career. Every time Professor R taught a class, he gave a special problem for the students to solve. You being his favourite student, put your heart into solving it one last time.

You are given two polynomials f(x)=a0+a1x+⋯+an−1xn−1 and g(x)=b0+b1x+⋯+bm−1xm−1, with positive integral coefficients. It is guaranteed that the cumulative GCD of the coefficients is equal to 1 for both the given polynomials. In other words, gcd(a0,a1,…,an−1)=gcd(b0,b1,…,bm−1)=1. Let h(x)=f(x)⋅g(x). Suppose that h(x)=c0+c1x+⋯+cn+m−2xn+m−2.

You are also given a prime number p. Professor R challenges you to find any t such that ct isn’t divisible by p. He guarantees you that under these conditions such t always exists. If there are several such t, output any of them.

As the input is quite large, please use fast input reading methods.

Input
The first line of the input contains three integers, n, m and p (1≤n,m≤106,2≤p≤109), — n and m are the number of terms in f(x) and g(x) respectively (one more than the degrees of the respective polynomials) and p is the given prime number.

It is guaranteed that p is prime.

The second line contains n integers a0,a1,…,an−1 (1≤ai≤109) — ai is the coefficient of xi in f(x).

The third line contains m integers b0,b1,…,bm−1 (1≤bi≤109) — bi is the coefficient of xi in g(x).

Output
Print a single integer t (0≤t≤n+m−2) — the appropriate power of x in h(x) whose coefficient isn’t divisible by the given prime p. If there are multiple powers of x that satisfy the condition, print any.

Examples
inputCopy
3 2 2
1 1 2
2 1
outputCopy
1
inputCopy
2 2 999999937
2 1
3 1
outputCopy
2
Note
In the first test case, f(x) is 2x2+x+1 and g(x) is x+2, their product h(x) being 2x3+5x2+3x+2, so the answer can be 1 or 2 as both 3 and 5 aren’t divisible by 2.

In the second test case, f(x) is x+2 and g(x) is x+3, their product h(x) being x2+5x+6, so the answer can be any of the powers as no coefficient is divisible by the given prime.

题意:给出fx和gx多项式 求hx = fxgx中某个系数%p不为0 输出任意满足条件系数位置均可
思路:如果数 x,y%p != 0 则有 a
b%p != 0 -> a%p*b%p != 0

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second

int main()
{
    int n,m,p;

    scanf("%d%d%d",&n,&m,&p);

    int a,b,x;

    for(int i = 1;i <= n;i ++)
    {
        scanf("%d",&x);

        if(x%p)
            a = i;
    }

    for(int i = 1;i <= m;i ++)
    {
        scanf("%d",&x);

        if(x%p)
            b = i;
    }

    printf("%d\n",a + b - 2);

    return 0;
}



发布了54 篇原创文章 · 获赞 0 · 访问量 1198

猜你喜欢

转载自blog.csdn.net/weixin_44144278/article/details/104687926