0
点赞
收藏
分享

微信扫一扫

Educational Codeforces Round 141

卿卿如梦 2023-01-09 阅读 164

题目:​​Educational Codeforces Round 141​​

Educational Codeforces Round 141_c++

A. Make it Beautiful

题意 :给出一个序列a,对于任意的a[i],若a[i]=前i-1项的和,则该序列“不漂亮”。先对a操作,若能将其变得漂亮,则输出YES;否则输出NO

思路:方式1:先将该序列排序,再将最大的数放置新序列的第一位即可。注意若该序列每个数相等,则不可能变漂亮。

AC_code:

#include<bits/stdc++.h>
using namespace std;
int a[55], sum, p;
//map<int, int> mp;
void solve(){
int n; cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}

sort(a + 1, a + n);
if(a[1] == a[n]){
cout << "NO" << endl;
return ;
}
a[0] = a[n];
cout << "YES" << endl;
for(int i = 0; i < n; i++){
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
int t; cin >> t;
while(t--) solve();
return 0;
}

B. Matrix of Differences

题意:构造出一个n*n的矩阵,要求使得所有相邻的数的差种类最多

思路:每一行的数分别从从1和n*n交替排列,即1 n*n 2 n*n-1 3 n*n-2 .....。并且按照蛇形矩阵进行各行的排列,即类似于如下排列:

Educational Codeforces Round 141_math_02

AC_code:

#include<bits/stdc++.h>
using namespace std;
int n, t, mp[55][55], res[55][55];
void solve(){
int n; cin >> n;
int l = 1, r = n * n;
for(int i = 1; i <= n; i++){
if(i % 2){
for(int j = 1; j <= n; j++){
if(j % 2) res[i][j] = l++;
else res[i][j] = r--;
}
}
else{
for(int j = n; j >= 1; j--){
if(j % 2) res[i][j] = r--;
else res[i][j] = l++;
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cout << res[i][j] << ' ';
}
cout << endl;
}
}
int main()
{
cin >> t;
while(t--) solve();
return 0;
}


举报

相关推荐

0 条评论