Virtual Judge周赛一//2023上半学年

  • A-Nicholas and Permutation:

Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n.

Nicholas want the minimum element (integer 1) and the maximum element (integer n) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions.

Input:

The first line of the input contains a single integer n (2 ≤ n ≤ 100) — the size of the permutation.

The second line of the input contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n), where ai is equal to the element at the i-th position.

Output:

Print a single integer — the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap.

Sample 1:

InputcopyOutputcopy
5
4 5 1 3 2
3

Sample 2:

InputcopyOutputcopy
7
1 6 5 3 4 7 2
6

Sample 3:

InputcopyOutputcopy
6
6 5 4 3 2 1
5

Note:

In the first sample, one may obtain the optimal answer by swapping elements 1 and 2.

In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2.

In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2.

Sponsor

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

int a[105];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int minx,maxx;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==1)
            {
                minx=i+1;
            }
            else if(a[i]==n)
            {
                maxx=i+1;
            }
        }
        int s1=abs(maxx-minx);
        int maxxx=max(maxx,minx)-1;
        int minxx=n-min(maxx,minx);
        int re=max(s1,max(maxxx,minxx));
        printf("%d\n",re);
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n;
    cin>>n;
    vector<int>v(n+1);
    int x1,xn;
    for(int i=1; i<=n; ++i)
    {
        cin>>v[i];
        if(v[i]==1)
            x1=i;
        else if(v[i]==n)
            xn=i;
    }
    int d1=xn-1;
    int d2=n-xn;
    int d3=n-x1;
    int d4=x1-1;
    cout<<max(max(d1,d2),max(d3,d4));
}
#include <bits/stdc++.h>
#define rep(_,__) for(_=1;_<=(__);_++)
using namespace std;
int main()
{
    int n, a, b, x, i;
    cin>>n;
    rep(i,n)
    {
        cin>>x;
        if(x==1)a=i;
        if(x==n)b=i;
    }
    if(a>b)swap(a,b);
    cout<<abs(a-b)+max(a-1,n-b);//注意此处!n-b+1,因为是从1处开始初始化!0开始
    return 0;
}

//利用_,__来变量同一;

//___ 是占位符,代表循环计数器和循环的上限值。下划线 _ 在C和C++中常被用作无意义的变量名,表示该变量的值不会在循环体内使用。

/*如果依照题目有如下缀叙:

1)开动态空间存储1-n;

2)依据题目中的一次交换

      以交换位置为分类讨论不断枚举

      /*分为min_num swap 1||n,max_numswap1||n四种情况并且

        max(max_len)*/        

      最终更新ans//初始化为1<<20,类无穷,已经到补码阈值;

*/

/*根据题目out,知道不用拿1-n的vector<int>数组来进行后续处理,out的只是

   len,而len由a,b双指针定义,即实际要找到数值为1时的下标a,找到

   数值为n时的下标b,并且在交换a,b下标时不改变ans的情况下进行情景简化;

*/

  • B-Square String?

A string is called square if it is some string written twice in a row. For example, the strings "aa", "abcabc", "abab" and "baabaa" are square. But the strings "aaa", "abaaab" and "abcdabc" are not square.

For a given string s determine if it is square.

Input

The first line of input data contains an integer t(1≤t≤100) —the number of test cases.

This is followed by t lines, each containing a description of one test case. The given strings consist only of lowercase Latin letters and have lengths between 11 and 100100 inclusive.

Output

For each test case, output on a separate line:

  • YES if the string in the corresponding test case is square,
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

Sample 1

InputcopyOutputcopy
10
a
aa
aaa
aaaa
abab
abcabc
abacaba
xxyy
xyyx
xyxy
NO
YES
NO
YES
YES
YES
NO
NO
NO

分治双指针滑动窗口同步遍历:

#include<bits/stdc++.h>
using namespace std;
int n;
string str;
int main(){
	cin>>n;
	while(n--){
		int cnt=0;
		cin>>str;
		int mid=str.length()+1>>1;
		for(int i=0;i<mid;i++){
			if(str[i]==str[mid+i])cnt++;
		}
		if(cnt==mid)cout<<"Yes"<<endl;
		else cout<<"NO"<<endl;
	}

	return 0;
}

//核心在于mid类分治,/*mid=str.length()+1>>1保险数字不下移*/

//双指针遍历比较,计数器变量统计,对偶则out“Yes”;

//子任务:输入字符串长度为奇数则out"No"

//主任务:mid分区双指针滑动窗口同步遍历;

//比赛时优先解决主任务,因为主任务可能同时解决子任务了;

  • C-Alex and broken contest:

One day Alex was creating a contest about his friends, but accidentally deleted it. Fortunately, all the problems were saved, but now he needs to find them among other problems.

But there are too many problems, to do it manually. Alex asks you to write a program, which will determine if a problem is from this contest by its name.

It is known, that problem is from this contest if and only if its name contains one of Alex's friends' name exactly once. His friends' names are "Danil", "Olya", "Slava", "Ann" and "Nikita".

Names are case sensitive.

Input

The only line contains string from lowercase and uppercase letters and "_" symbols of length, not more than 100 — the name of the problem.

Output

Print "YES", if problem is from this contest, and "NO" otherwise.

Sample 1

InputcopyOutputcopy
Alex_and_broken_contest
NO

Sample 2

InputcopyOutputcopy
NikitaAndString
YES

Sample 3

InputcopyOutputcopy
Danil_and_Olya
NO

暴力遍历:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
int main()
{
    string a="Danil",b="Olya",c="Slava",d="Ann",e="Nikita";
    string s;
    int sum;
    while(cin>>s)
    {
        sum=0;
        int aa=0,bb=0,cc=0,dd=0,ee=0;
        for(int i=0; i<s.size(); i++)
        {
            if(s[i]=='D'&&s[i+1]=='a'&&s[i+2]=='n'&&s[i+3]=='i'&&s[i+4]=='l')
            {
                aa++;
            }
            if(s[i]=='O'&&s[i+1]=='l'&&s[i+2]=='y'&&s[i+3]=='a')
            {
                bb++;
            }
            if(s[i]=='S'&&s[i+1]=='l'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='a')
            {
                cc++;
            }
            if(s[i]=='A'&&s[i+1]=='n'&&s[i+2]=='n')
            {
                dd++;
            }
            if(s[i]=='N'&&s[i+1]=='i'&&s[i+2]=='k'&&s[i+3]=='i'&&s[i+4]=='t'&&s[i+5]=='a')
            {
                ee++;
            }
 
        }
        sum=aa+bb+cc+dd+ee;
        if(sum==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

substr()分支函数:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
string str[] = { "Danil", "Olya", "Slava", "Ann", "Nikita" };
string S;
int main()
{
    cin >> S;
    int flag = 0;
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < S.size(); j++)
        {
            if (S.substr(j, str[i].size()) == str[i])//依次遍历字符串S,找到该串j位置到str[i]长度位置中符合str[i]的字符串
            {
                flag++;
            }
        }
    }
    if (flag == 1)
    {
        cout<<"YES"<<endl;
    }
    else
    {
        cout<<"NO"<<endl;
    }
    return 0;
}

find()和rfind()函数:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
string str[] = { "Danil", "Olya", "Slava", "Ann", "Nikita" };
string S;
int main()
{
 
	string str[5] = {"Danil", "Olya", "Slava", "Ann","Nikita" };
	string ss;
	cin >> ss;
	int cnt = 0;
	bool isok = false;
	for (int i = 0; i < 5; ++i) {
		int p = ss.find(str[i]);
		int q = ss.rfind(str[i]);
		if (p == -1 || q == -1)	continue;//不含有这个朋友的名字
		if (p == q)	isok = true;//表明只含一个这个字符串
		cnt++;//记录含有多少个朋友的名字
	}
	if (cnt == 1 && isok)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
    return 0;
}

/*解释函数:

 string str ("The sixth sick sheik's sixth sheep's sick.");
    string key ("sixth");
    int a=str.find(key);//返回左边子串的第一个元素的位置4
    int b=str.rfind(key);//返回右边子串的第一个元素的位置23
    cout<<a<<" "<<b<<endl;

*/

  • D - Nikita and string:

One day Nikita found the string containing letters "a" and "b" only.

Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".

Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?

Input

The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".

Output

Print a single integer — the maximum possible size of beautiful string Nikita can get.

Sample 1

InputcopyOutputcopy
abba
4

Sample 2

InputcopyOutputcopy
bab
2

Note

It the first sample the string is already beautiful.

In the second sample he needs to delete one of "b" to make it beautiful.

区间和前缀和,字符串区间用substr函数:

#include<bits/stdc++.h>
using namespace std;
int a[5010],b[5010];
int main()
{
    string s;
	cin>>s;
    int l=s.size();
    for(int i=1;i<=l;i++) //计算前缀
    {
        a[i]=a[i-1]+(s[i-1]=='a');
        b[i]=b[i-1]+(s[i-1]=='b');
    }
    int maxl=0;
    for(int i=0;i<=l;i++)//不取i=1因为相减时会有边界问题;
        for(int j=i;j<=l;j++)//若i=1,以b[j]-b[i]为例则在边界处j=1时得不到它在下标1处的值
            maxl=max(maxl,a[i]+(b[j]-b[i])+(a[l]-a[j])); 
    printf("%d\n",maxl);
}

//注意检查边界;

  • E - Two distinct points :

You are given two segments [l1;r1] and [l2;r2] on the x-axis. It is guaranteed that l1<r1 and l2<r2. Segments may intersect, overlap or even coincide with each other.

The example of two segments on the x-axis.

Your problem is to find two integers a and b such that l1≤a≤r1, l2≤b≤r2 and a≠b. In other words, you have to choose two distinct integer points in such a way that the first point belongs to the segment [l1;r1] and the second one belongs to the segment [l2;r2].

It is guaranteed that the answer exists. If there are multiple answers, you can print any of them.

You have to answer q independent queries.

Input

The first line of the input contains one integer q (1≤q≤500) — the number of queries.

Each of the next q lines contains four integers l1i,r1i,l2i and r2i (1≤l1i,r1i,l2i,r2i≤10e+9,l1i<r1i,l2i<r2i) — the ends of the segments in the i-th query.

Output

Print 2q integers. For the i-th query print two integers ai and bi — such numbers that l1i≤ai≤r1i, l2i≤bi≤r2i and ai≠bi. Queries are numbered in order of the input.

It is guaranteed that the answer exists. If there are multiple answers, you can print any.

Sample 1

InputcopyOutputcopy
5
1 2 1 2
2 6 3 4
2 4 1 3
1 2 1 3
1 4 5 8
2 1
3 4
3 2
1 2
3 7

Sponsor

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int q, l1, r1, l2, r2;
    cin >> q;
    while(q--){
        cin >> l1 >> r1 >> l2 >> r2;
        if(l1!=l2) cout << l1 << " "<< l2 << endl;
        else cout << r1 << " "<< l2 << endl;
    }

	return 0;
}
  • F - Divisors of Two Integers:

Recently you have received two positive integer numbers x and y. You forgot them, but you remembered a shuffled list containing all divisors of x (including 11 and x) and all divisors of y (including 11 and y). If d is a divisor of both numbers x and y at the same time, there are two occurrences of d in the list.

For example, if x=4 and y=6 then the given list can be any permutation of the list [1,2,4,1,2,3,6][1,2,4,1,2,3,6]. Some of the possible lists are: [1,1,2,4,6,3,2][1,1,2,4,6,3,2], [4,6,1,1,2,3,2][4,6,1,1,2,3,2] or [1,6,3,2,4,1,2][1,6,3,2,4,1,2].

Your problem is to restore suitable positive integer numbers x and y that would yield the same list of divisors (possibly in different order).

It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers x and y.

Input

The first line contains one integer n (2≤n≤128) — the number of divisors of x and y.

The second line of the input contains n integers d1,d2,… (1≤di≤104), where di is either divisor of x or divisor of y. If a number is divisor of both numbers x and y then there are two copies of this number in the list.

Output

Print two positive integer numbers x and y — such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.

Sample 1

InputcopyOutputcopy
10
10 2 8 1 2 4 1 20 4 5
20 8

Sponsor

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
 
using namespace std;
int a[150];
int b[150];
int vis[10005];
int main()
{
	
	int n;
	cin>>n;
	for(int t=0;t<n;t++)
	{
		scanf("%d",&a[t]);
	}
	sort(a,a+n);
	int l=a[n-1];
	int s=0;
 
	for(int t=0;t<n;t++)
	{
		if(l%a[t]==0&&vis[a[t]]==0)
		{
			vis[a[t]]=1;
			continue;
		}
		else
		{
			b[s++]=a[t];
		}
	}
	sort(b,b+s);
	cout<<l<<" "<<b[s-1]<<endl;
	
	
	return 0;
}
  • G - Polycarp and Sums of Subsequences :

Polycarp had an array a of 33 positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array b of 77 integers.

For example, if a={1,4,3}, then Polycarp wrote out 11, 44, 33, 1+4=51+4=5, 1+3=41+3=4, 4+3=74+3=7, 1+4+3=81+4+3=8. After sorting, he got an array b={1,3,4,4,5,7,8}.

Unfortunately, Polycarp lost the array a. He only has the array b left. Help him to restore the array a.

Input

The first line contains one integer t (1≤t≤5000) — the number of test cases.

Each test case consists of one line which contains 77 integers b1,b2,…,b7 (1≤bi≤109; bi≤bi+1).

Additional constraint on the input: there exists at least one array a which yields this array b as described in the statement.

Output

For each test case, print 33 integers — a1, a2 and a3. If there can be several answers, print any of them.

Sample 1

InputcopyOutputcopy
5
1 3 4 4 5 7 8
1 2 3 4 5 6 7
300000000 300000000 300000000 600000000 600000000 600000000 900000000
1 1 2 999999998 999999999 999999999 1000000000
1 2 2 3 3 4 5
1 4 3
4 1 2
300000000 300000000 300000000
999999998 1 1
1 2 2

Note

The subsequence of the array a is a sequence that can be obtained from a by removing zero or more of its elements.

Two subsequences are considered different if index sets of elements included in them are different. That is, the values of the elements don't matter in the comparison of subsequences. In particular, any array of length 33 has exactly 77 different non-empty subsequences.

#include <bits/stdc++.h>

using namespace std;

int a[100];

void solve() {
	for (int i = 0; i < 7; ++i) {
		cin >> a[i];
	}
	cout << a[0] << ' ' << a[1] << ' ' << a[6] - a[0] - a[1] << '\n';
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	ios::sync_with_stdio(false);
	cin.tie(nullptr);					//关同步,下同
	cout.tie(nullptr);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值