A. Ehab Fails to Be Thanos
You’re given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn’t equal to the sum of the last n elements?
Input
The first line contains an integer n (1≤n≤1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a1, a2, …, a2n (1≤ai≤106) — the elements of the array a.
Output
If there’s no solution, print “-1” (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
input
3
1 2 2 1 3 1
output
2 1 3 1 1 2
input
1
1 1
output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren’t equal.
In the second example, there’s no solution.
Analysis
这道题的意思是给出2n的数字的一种排列,满足前n的数的和与后n个数的和不相等,如果不存在这样的排列则输出-1。
稍作分析可知,唯一输出-1的情况是所有的数都相等,不如先排序,然后判断首尾是否相等,相等则输出-1,否则输出排序结果。
Code
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int a[2 * n];
for (int i = 0; i < 2 * n; i++){
cin>>a[i];
}
sort(a, a + 2 * n);
if (a[0] == a[2 * n - 1]) {
cout<<-1<<endl;
} else {
for (int i = 0; i < 2 * n; i++) {
cout<<a[i]<<" ";
}
}
return 0;
}
B. Ehab Is an Odd Person
You’re given an array a of length n. You can perform the following operation on it as many times as you want:
· Pick two integers i and j (1≤i,j≤n) such that ai+aj is odd, then swap ai and aj.
What is lexicographically the smallest array you can obtain?
An array x is lexicographically smaller than an array y if there exists an index i such that xi<yi, and xj=yj for all 1≤j<i. Less formally, at the first index i in which they differ, xi<yi
Input
The first line contains an integer n (1≤n≤105) — the number of elements in the array a.
The second line contains n space-separated integers a1, a2, …, an (1≤ai≤109) — the elements of the array a.
Output
The only line contains n space-separated integers, the lexicographically smallest array you can obtain.
Examples
input
3
4 1 7
output
1 4 7
input
2
1 1
output
1 1
Note
In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
Analysis
这道题的意思是如果两个数的和是奇数,也就是奇偶性不同,则可以交换,否则不能交换。稍作分析可知,只要不是全为偶数或全为奇数,都可以通过交换随意排序。另外要满足题目中的字典序最小。所以判断一下如果全是偶数或全是奇数则直接输出,否则排序输出。
Code
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int a[n];
bool flag1 = false;
bool flag2 = false;
for (int i = 0; i < n; i++) {
cin>>a[i];
if (a[i] % 2 == 0)
flag1 = true;
else
flag2 = true;
}
if (flag1 && flag2)
sort(a, a + n);
for (int i = 0; i < n; i++) {
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
C. Ehab and a Specia
l Coloring Problem
You’re given an integer n. For every integer i from 2 to n, assign a positive integer ai such that the following conditions hold:
· For any pair of integers (i,j), if i and j are coprime, ai≠aj.
· The maximal value of all ai should be minimized (that is, as small as possible).
A pair of integers is called coprime if their greatest common divisor is 1.
Input
The only line contains the integer n (2≤n≤105).
Output
Print n−1 integers, a2, a3, …, an (1≤ai≤n).
If there are multiple solutions, print any of them.
Examples
input
4
output
1 2 1
input
3
output
2 1
Note
In the first example, notice that 3 and 4 are coprime, so a3≠a4. Also, notice that a=[1,2,3] satisfies the first condition, but it’s not a correct answer because its maximal value is 3.
Analysis
这个题用到了筛法求素数,对于这个序列,其下标如果互质那么两者不能相等,并且要令数值尽可能地小。数组首先默认值为0, 然后设置一个pos,然后遍历,并通过++pos赋值,赋值后将n以内其所有倍数都赋值为相同的数。遍历过程中如果非零,则continue,这样可以实现下标互质数值不相等。遍历结束可以得到题目要求的序列,输出即可。
Code
#include<bits/stdc++.h>
using namespace std;
int main () {
int n;
cin>>n;
int a[n + 1];
for (int i = 2; i <= n; i++)
a[i] = 0;
int pos = 0;
for (int i = 2; i <= n; i++) {
if (a[i] != 0)
continue;
a[i] = ++pos;
int j = 2 * i;
while (j <= n) {
a[j] = pos;
j += i;
}
}
for (int i = 2; i <= n; i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
D、E、F还没做……
刚刚开始练习算法题,这里还是总结一点心得:
例如cpp可以直接#include<bits/stdc++.h>,会把标准库都引用,写法比较简单。做的这三道题与数据结构或者算法关系都不大,但是要注意其中的一些数学思想,要把题干要求做一个小的转换,找到其数学本质。另外要总结一些常用的方法,例如筛法求素数。