Codeforces Round #735 (Div. 2)(A~D)

A. Cherry(https://codeforces.com/contest/1554/problem/A)
You are given n integers a1,a2,…,an. Find the maximum value of max(al,al+1,…,ar)⋅min(al,al+1,…,ar) over all pairs (l,r) of integers for which 1≤l<r≤n.
Input
The first line contains a single integer t (1≤t≤10000) — the number of test cases.

The first line of each test case contains a single integer n (2≤n≤105).

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤106).

It is guaranteed that the sum of n over all test cases doesn’t exceed 3⋅105.

Output
For each test case, print a single integer — the maximum possible value of the product from the statement.
Example
inputCopy
4
3
2 4 3
4
3 2 3 1
2
69 69
6
719313 273225 402638 473783 804745 323328
outputCopy
12
6
4761
381274500335
Note
Let f(l,r)=max(al,al+1,…,ar)⋅min(al,al+1,…,ar).
In the first test case,
f(1,2)=max(a1,a2)⋅min(a1,a2)=max(2,4)⋅min(2,4)=4⋅2=8.
f(1,3)=max(a1,a2,a3)⋅min(a1,a2,a3)=max(2,4,3)⋅min(2,4,3)=4⋅2=8.
f(2,3)=max(a2,a3)⋅min(a2,a3)=max(4,3)⋅min(4,3)=4⋅3=12.
So the maximum is f(2,3)=12.
In the second test case, the maximum is f(1,2)=f(1,3)=f(2,3)=6…

题意:给定一个长度为n的序列,在序列的连续子段(Al,Al+1,…Ar)中,找Al…Ar的最大值以及以及最小值,两者相乘,假设结果为res,求解所有子段中res的最大值。

我们并不需要枚举所有长度的字段,只需要将相邻元素两两相乘,不断更新res即可,也即我们不需要考虑长度大于2的子段。这样做的理由是:若有长度为3的子段a0,a1,a3则结果只能是a0a1和a1a2中较大的一个。假设a1是三者中最大或者最小的元素,上述结果显然成立,假如a1不是最大元素,则最优解应当是a1*max(a0,a2),综上,长度为3时,最优值只会在a0*a1和a1*a2中产生。长度大于3时同理。

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;
    ll res=-1;
    ll l,r;
    cin>>l;
    for(int i=1;i<n;i++){
        cin>>r;
        res=max(res,l*r);
        l=r;
    }
    cout<<res;
    return 0; 
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

B. Cobb(https://codeforces.com/contest/1554/problem/B)
You are given n integers a1,a2,…,an and an integer k. Find the maximum value of i⋅j−k⋅(ai|aj) over all pairs (i,j) of integers with 1≤i<j≤n. Here, | is the bitwise OR operator.

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

The first line of each test case contains two integers n (2≤n≤105) and k (1≤k≤min(n,100)).

The second line of each test case contains n integers a1,a2,…,an (0≤ai≤n).

It is guaranteed that the sum of n over all test cases doesn’t exceed 3⋅105.

Output
For each test case, print a single integer — the maximum possible value of i⋅j−k⋅(ai|aj).

Example
inputCopy
4
3 3
1 1 3
2 2
1 2
4 3
0 1 2 3
6 6
3 2 0 0 5 6
outputCopy
-1
-4
3
12
Note
Let f(i,j)=i⋅j−k⋅(ai|aj).

In the first test case,

f(1,2)=1⋅2−k⋅(a1|a2)=2−3⋅(1|1)=−1.
f(1,3)=1⋅3−k⋅(a1|a3)=3−3⋅(1|3)=−6.
f(2,3)=2⋅3−k⋅(a2|a3)=6−3⋅(1|3)=−3.
So the maximum is f(1,2)=−1.

In the fourth test case, the maximum is f(3,4)=12.

题意:给定n,k以及长度为n的序列,定义一个函数f(i,j)=ij-k(ai|aj);求f(i,j)在1<=i<j<=n的最大值根据题目所给数据范围,0<=ai<=n,则根据或运算性质,ai|aj<2*n,k是固定的常数,在f(i,j)中能影响到f值的只有i,j,假设我们选取最大的i,j,有i*j=n*(n-1);f(n-1,n)=n*(n-1)-k*(ai|aj),根据ai|aj<2*n,则有f(n-1,n)>n*(n-1)-2*n*k=n^2-2nk-n,我们考虑假设任意一个最优的f(i,j),即当ai=aj=0时f(i,j)=i*j-k*0=i*j;再假设j=n则f(i,j)最大取值为i*n,故,若想取得f(i,j)>f(n-1,n)至少需要满足i*n>n^2-2nk-n 即 i>n-2*k-1 也即 当i<=n-2k-1时,取值不可能优于f(n-1,n),因此我们只需遍历i>=n-2*k的所有取值,找到最优解

AC代码:

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 a[N];
int solve()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    ll res=(ll)n*(n-1)-(ll)k*(a[n]|a[n-1]);
    for(int i=(n-2*k<1?1:n-2*k);i<=n;i++){
        for(int j=i+1;j<=n;j++){
            res=max(res,(ll)i*j-(ll)k*(a[i]|a[j]));
        }
    }
    cout<<res;
    return 0; 
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

C. Mikasa(https://codeforces.com/contest/1554/problem/C)
You are given two integers n and m. Find the MEX of the sequence n⊕0,n⊕1,…,n⊕m. Here, ⊕ is the bitwise XOR operator.

MEX of the sequence of non-negative integers is the smallest non-negative integer that doesn’t appear in this sequence. For example, MEX(0,1,2,4)=3, and MEX(1,2021)=0.

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

The first and only line of each test case contains two integers n and m (0≤n,m≤109).

Output
For each test case, print a single integer — the answer to the problem.

Example
inputCopy
5
3 5
4 6
3 2
69 696
123456 654321
outputCopy
4
3
0
640
530866
Note
In the first test case, the sequence is 3⊕0,3⊕1,3⊕2,3⊕3,3⊕4,3⊕5, or 3,2,1,0,7,6. The smallest non-negative integer which isn’t present in the sequence i. e. the MEX of the sequence is 4.

In the second test case, the sequence is 4⊕0,4⊕1,4⊕2,4⊕3,4⊕4,4⊕5,4⊕6, or 4,5,6,7,0,1,2. The smallest non-negative integer which isn’t present in the sequence i. e. the MEX of the sequence is 3.

In the third test case, the sequence is 3⊕0,3⊕1,3⊕2, or 3,2,1. The smallest non-negative integer which isn’t present in the sequence i. e. the MEX of the sequence is 0.

题意:给定n和m,找出没有出现在n⊕0,n⊕1,…,n⊕m 中的最小非负整数
假设x为上述序列中的某一个数,则存在一个数y满足0<=y<=m,使得 n XOR y = x ,则有0<= n XOR x = y <=m ,因此本问题转化为找到满足n XOR x > m 的最小非负整数。我们只需从n,m的高位开始,若m最高位为1而n的为0,则我们将n的这一位补齐,循环操作直至n>m操作结束

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,m;
    cin>>n>>m;
    m++;
    int nn,nm,res=0;
    for(int i=30;i>=0&&n<m;i--)
    {
        nn=(n>>i&1);nm=(m>>i&1);
        if(nn>=nm) continue;
        else{
            res|=(1<<i);
            n|=(1<<i);
        }
    }
    cout<<res;
    return 0; 
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

D. Diane(https://codeforces.com/contest/1554/problem/D)
You are given an integer n. Find any string s of length n consisting only of English lowercase letters such that each non-empty substring of s occurs in s an odd number of times. If there are multiple such strings, output any. It can be shown that such string always exists under the given constraints.

A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

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

The first line of each test case contains a single integer n (1≤n≤105).

It is guaranteed that the sum of n over all test cases doesn’t exceed 3⋅105.

Output
For each test case, print a single line containing the string s. If there are multiple such strings, output any. It can be shown that such string always exists under the given constraints.

Example
inputCopy
4
3
5
9
19
outputCopy
abc
diane
bbcaabbba
youarethecutestuwuu
Note
In the first test case, each substring of “abc” occurs exactly once.

In the third test case, each substring of “bbcaabbba” occurs an odd number of times. In particular, “b” occurs 5 times, “a” and “bb” occur 3 times each, and each of the remaining substrings occurs exactly once.

题意:给定数字n,求解一个长度为n的字符串,使得该字符串的所有子串出现奇数次

观察全是同一个字母的序列:aa…a,假设有奇数个a,设有k个a,则子串a出现次数为奇数次(k次),子串aa出现次数为偶数次,aaa出现次数为奇数次…依次类推,若有k-1(偶数)个a,则恰好相反,即a , aa ,aaa … 出现次数分别为偶数、奇数、偶数…并且,关于奇偶数的一个结论为:奇数+偶数=奇数,于是我们可以把奇数个a和偶数个a组合起来,中间再用一个其他字母分隔开,例如,当n等于6时我们可以构造出aabaaa,若n为奇数例如n=7时
我们可以构造aaabcaa

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;
    if(n==1){
        cout<<'a'<<endl;
        return 0;
    }
    string s="";
    if(n&1){
        for(int i=0;i<(n-1)/2;i++) s+="a";
        s+="bc";
        for(int i=0;i<(n-1)/2-1;i++) s+="a";
    }
    else{
        for(int i=0;i<n/2;i++) s+="a";
        s+="b";
        for(int i=0;i<n/2-1;i++) s+="a";
    }
    cout<<s;
    return 0; 
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值