A. Ehab Fails to Be Thanos
题意:给一个长度为
2
n
2n
2n的数组,问是不是能否重排数组,使得数组前
n
n
n个数之和和后
n
n
n个数之和不同
解:如果数组每个元素都相同,那么就肯定不能了,反之就直接一个sort即可
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll MAX = 1e3+5;
ll arr[MAX << 1];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
for(int i = 0; i < (n<<1); ++i){
cin >> arr[i];
}
sort(arr,arr+2*n);
ll tot = 0,tot2= 0;
for(int i = 0; i < n; ++i){
tot+= arr[i];
tot2 += arr[i+n];
}
if(tot==tot2){
cout << -1 << endl;
}
else{
for(int i = 0; i < 2*n; ++i){
if(i) cout << ' ';
cout << arr[i];
if(i==2*n-1) cout << endl;
}
}
return 0;
}
B题:
题意: 定义一种操作:Pick two integers i and j (1≤i,j≤n) such that
a
i
a_i
ai+
a
j
a_j
aj is odd, then swap
a
i
a_i
ai and
a
j
a_j
aj.
即在数组中选出两个数,如果两者相加的是奇数,那么就可以交换,问能否使得该数组有序。
解:
如果一个数组里只有奇数或者只有偶数,那么数组就不能交换,所以就直接输出即可了
反之只要一个数组里又有奇数又有偶数,那么就能随意交换,所以sort之后输出即可。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const ll MAX = 1e5+5;
ll arr[MAX];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
bool haveodd = false,haveeven = false;
for(int i = 0; i < n; ++i){
cin >> arr[i];
if(arr[i]&1) haveodd = true;
if(arr[i]%2==0) haveeven = true;
}
if(haveodd && haveeven){
sort(arr,arr+n);
for(int i = 0; i < n; ++i){
if(i) cout << ' ';
cout << arr[i];
if(i==n-1) cout << endl;
}
}
else{
for(int i = 0; i < n; ++i){
if(i) cout << ' ';
cout << arr[i];
if(i==n-1) cout << endl;
}
}
return 0;
}
C题:
题意: 构造一个序列
a
a
a,如果其中的
i
i
i与
j
j
j 是互质的,满足
a
i
!
=
a
j
a_i != a_j
ai!=aj,还得使
a
i
a_i
ai尽量小
#include <iostream>
using namespace std;
const int MAX = 1e5+5;
bool prime[MAX];
int a[MAX];
int main()
{
int n; cin >> n;
int cnt = 0;
for(int i = 2; i*i <= n; ++i){
if(!prime[i]){
a[i] = ++cnt;
for(int j = i*i; j <= n; j+=i) prime[j] = true,a[j] = cnt;
}
}
for(int i = 2; i <= n; ++i){
if(i>2) cout << ' ';
cout << (a[i] ? a[i] : ++cnt);
}
cout << endl;
return 0;
}