day6: 这次的题目都是构造题,关键是找到正确的构造策略(要大胆假设,小心求证,虽然有时正确性在考场上没时间证明)。可以通过枚举小的正确样例来推导可行的构造策略。
A: 分n个硬币到两堆(可以翻转后放入),其中k个为反面。考虑先找小样例,发现前k个硬币正着放到一堆,其他硬币反着放到另一堆,是一个合理方案。在前k个中,有a个硬币(a<=k)为反,则放入后a个为正。后n-k个硬币中,有(k-a)个硬币为反,则放入后a个硬币为为正。此题重点在利用共k个硬币为反的关系(给了K肯定和k有关吗)。
B: 构造一个长度为n 的排列a,构造a使前i个数的gcd(A(i),A(i-1)+1)的和最小。这个题题如其名,把偶数按顺序全部提到前面,再把其它奇数放到前面即可,因为除了1和n,其他都是相邻的自然数,gcd = 1,必定最小。
C: 水题,把50个元素大小变成a,45个元素大小变成b,4个元素大小变为c,最后一个c+1,构造一个n = 100的数据集即可。
D: 将一个长度为 n×m 的排列填入一个 n×m 矩阵A中,使得任何两个相邻元素的和都是唯一的。考虑对角线填充(主对角线递增)可证明构造一定正确。还有一种在列数为奇数时将偶数行循环左移一位将奇数偶数值域分离的方法,较难证明,但是也可以过。
F: 找到满足以下约束条件的字典序最小排列,对于所有0 <= i <= n, p0⊕p1⊕⋯⊕pi >0。是按位异或操作。易发现当n = 0或 n%4 == 0时没有这种排列,其他情况下将4i-1 和 4i交换顺序即可,可保证构造方式最优。
G: 水题,将数组分为n,n+1两堆,问什么时候前面数组的积恒大于后面数组的积。发现元素可以为负,考虑正负性,发现n为偶时全为-1即可,n为奇时把一个数替换为2,则在2分到前面时,前面为2,后面为1;2分到后面时,前为-1,后为-2。所以构造成立。
H: n行n列的网格。网格中每个单元格都填有一个整数,至少满足以下条件之一:存在至少一行,该行所有单元格的数值都不大于x;存在至少一列,该列所有单元格的数值都不大于x。考虑k的范围,易知n <= k <= n*n-n+1,把最大的3个数一个占一行一列,k再占一行一列,保证一行元素最大为k就行。
整个题单还算难,构造题还是需要思路上的锤炼。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++) {
if(i <= k) cout << "1";
else cout << "4";
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
return 0;
}#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
int n;
cin >> n;
for(int i = 2; i <= n; i++) {
if((i & 1) == 0) cout << i << ' ';
}
for(int i = 1; i <= n; i++) {
if((i & 1) == 1) cout << i << ' ';
}
cout << '\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) {
work();
}
return 0;
}#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
ll n = 100;
vector<int> v(n+1, 0);
ll a, b, c;
cin >> a >> b >> c;
int na = n/2, nb = n*95/100, nc = n*99/100;
for(int i = 1; i <= na; i++) v[i] = a;
for(int i = na + 1; i <= nb; i++) v[i] = b;
for(int i = nb + 1; i <= nc; i++) v[i] = c;
for(int i = nc + 1; i <= n; i++) v[i] = c+1;
cout << "100" << '\n';
for(int i = 1; i <= n; i++) cout << v[i] << ' ';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
return 0;
}#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n+1, vector<int>(m+1, 0));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
a[i][j] = (i-1)* m + j;
}
}
cout << "Yes" << '\n';
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(m%2!=0 && i%2==0 && j!=m) cout << a[i][j+1] << ' ';
else if(m%2!=0 && i%2==0 && j==m)cout << a[i][1] << ' ';
else cout << a[i][j] << ' ';
}
cout << '\n';
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) {
work();
}
return 0;
}#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
int t;
cin >> t;
int n = (int)1e6;
vector<int> a(n, 0);
for(int i = 0; i < n; i++) a[i] = i;
swap(a[0], a[1]);
for(int i = 1; i < n/4; i++) {
swap(a[i*4], a[i*4-1]);
}
while(t--) {
int x;
cin >> x;
if(x%4 == 0|| x == 1) cout << "impossible" << '\n';
else {
for(int j = 0; j < x; j++) {
cout << a[j] << ' ';
}
cout << '\n';
}
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
int x, n;
cin >> x;
n = x*2 + 1;
vector<int> a(n);
if(x % 2 == 0) {
for(int i = 0; i < n; i++) a[i] = -1;
} else if(x % 2 == 1) {
a[0] = 2;
for(int i = 1; i < n; i++) a[i] = -1;
}
cout << "YES" << '\n';
for(auto i : a) cout << i << ' ';
cout << '\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) {
work();
}
return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
int n, k;
cin >> n >> k;
vector<vector<int>> g(n+1, vector<int>(n+1, 0));
if(k < n|| k > n*n - n + 1) {
cout << "No" << '\n';
return;
}
int cnt = n*n;
g[1][n] = k;
for(int i = n; i >= 2; i--) {
for(int j = 1; j <= n-1; j++) {
if(i + j == n+1) g[i][j] = cnt--;
}
}
cnt = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(g[i][j]) continue;
if(cnt == k-1) {
g[i][j] = cnt + 2; cnt += 2;
}
else g[i][j] = ++cnt;
}
}
cout << "Yes" << '\n';
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cout << g[i][j] << ' ';
}
cout << '\n';
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) {
work();
}
return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
int n;
cin >> n;
vector<vector<int>> g(n+1, vector<int>(n+1, 0));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i <= n-2) g[i][j] = i;
else {
if(j <= n-2) g[i][j] = n-2+j;
else if(i == n-1) g[i][j] = i + j - 1;
else g[i][j] = i + j;
}
}
}
cout << "Yes" << '\n';
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cout << g[i][j] << ' ';
}
cout << '\n';
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
return 0;
}
C++