返回

Educational Codeforces Round 141

发布时间:2023-04-25 05:55:20 312
# c++

题目:​​Educational Codeforces Round 141​​

Educational Codeforces Round 141_构造

A. Make it Beautiful

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

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

AC_code:

#include
using namespace std;
int a[55], sum, p;
//map 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_构造_02

AC_code:

#include
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)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
初识C语言 2023-04-25 02:53:59