说明
题目链接
本场div2是构造场,A~D全是构造,我因为一些失误,只做出了A。本来B已经做出来了,结果因为听歌的时候没有注意到我已经导出了正解,白白送掉了。于是全程想B,最后掉了大分,从绿名掉到灰名。
A SSeeeeiinngg DDoouubbllee
正输,反输。
// 11.25 cf div2.cpp : 定义控制台应用程序的入口点。
//
//#include "stdafx.h"
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t; cin >> t;
while(t--) {
string s;
cin >> s;
int len = s.length();
for (int i = 0; i < len; i++) {
cout << s[i];
}
for (int i = len - 1; i >= 0; i--) {
cout << s[i];
}
cout << endl;
}
//system("pause");
return 0;
}
B XOR = Average
找到一种特殊的构造方法就可,不是非要令这平均值是n
n-2个4,2,6也可以
// 11.25 cf div2.cpp : 定义控制台应用程序的入口点。
//
//#include "stdafx.h"
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t; cin >> t;
while(t--) {
int n; cin >> n;
if (n & 1) {
for (int i = 1; i <= n; i++) {
cout << 1 << " "[i == n];
}
}
else {
for (int i = 1; i <= n - 2; i++) {
cout << 4 << " ";
}
cout << 2 << " " << 6;
}
cout << endl;
}
//system("pause");
return 0;
}
C Almost All Multiples
n=9,x=5
1 2 3 4 5 6 7 8 9
5 2 3 4 1 6 7 8 9
5 2 3 4 9 6 7 8 1
实质,x位置的数换成n
因此n必须能整除x
n=9,x=3
1 2 3 4 5 6 7 8 9
3 2 1 4 5 6 7 8 9
3 2 9 4 5 6 7 8 1最差情况
最开始必然是第一个满足位
向后找
n=12,x=3
3 2 12 4 5 6 7 8 9 10 11 1
3 2 6 4 5 12 7 8 9 10 11 1
尽可能把小的往前换
// 11.25 cf div2.cpp : 定义控制台应用程序的入口点。
//
//#include "stdafx.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int ans[maxn];
int main()
{
int t; cin >> t;
while(t--) {
int n, x;
cin >> n >> x;
if (x == n) {
cout << x << " ";
for (int i = 2; i < n; i++) {
cout << i << " ";
}
cout << 1;
}
else if (n%x)cout << -1;
else {
for (int i = 1; i <= n; i++)ans[i] = i;
ans[1] = x, ans[n] = 1, ans[x] = n;
int cur = x;
for (int i = x + 1; i < n; i++) {
if (i%cur == 0 && n%i == 0) {
swap(ans[i], ans[cur]);
cur = i;
}
}
for (int i = 1; i <= n; i++)cout << ans[i] << " "[i == n];
}
cout << endl;
}
//system("pause");
return 0;
}
D Range = √Sum
n=6
3 4 5 7 8 9 sum = 36 sqrt = 6 max - min = 6
n-n/2 n-2 n-1 n+1 n+2 …… n+n/2
n=9 考虑构造n+1=10的局面 奇数n构造不出来,考虑构造偶数
n=10:
5 6 7 8 9 11 12 13 14 15
n=9:
6 7 8 9 12 13 14 15 16 sum = 100 max - min = 10
考虑将10个数缩减为9个数,且和不变,极差不变
方法:将n=10的某个数分配给一系列数,且和仍为100
发现将最小的5拿掉,极差变为9,那么要让极差仍为10
5就要分配个1给15,使其为16,然后剩下分配给14,13,12,11,就构造成功了
通解:
[(n+1)-(n+1)/2]+1 …… [(n+1)-1] [(n+1)+1]+1 …… [(n+1)+(n+1)/2]+1
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;cin>>t;
while(t--){
int n;cin>>n;
if(n&1){
n++;
for(int i=n/2+1;i<n;i++)cout<<i<<" ";
for(int i=n+2;i<=n+n/2+1;i++)cout<<i<<" "[i==(n+n/2+1)];
}
else{
for(int i=n/2;i<n;i++)cout<<i<<" ";
for(int i=n+1;i<=n+n/2;i++)cout<<i<<" "[i==(n+n/2)];
}
cout<<endl;
}
return 0;
}
总结
太久没练构造题,手生了,本来构造是我的长项,结果现在被这套div2啪啪打脸。