这一场完成了前三题加了分, 收获在于熟悉了优先队列以及运算符的重载(在D题中有所体现)
A. Most Unstable Array(数学)
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long
using namespace std;
const int N = 10e5;
int main(){
FAST;
rush(){
ll n,m;
cin>>n>>m;
if(n == 1)cout<<"0"<<endl;
else if(n == 2)cout<<m<<endl;
else cout<<2 * m<<endl;
}
return 0;
}
B. Two Arrays And Swaps(字符串)
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long
using namespace std;
const int N = 10e5;
int a[N],b[N];
int main(){
FAST;
rush(){
int n,k;
cin>>n>>k;
for(int i = 0;i < n;i++)cin>>a[i];
for(int i = 0;i < n;i++)cin>>b[i];
sort(a,a + n);
sort(b,b + n);
int high = n - 1,low = 0;
while(k-- && low < n){
if(a[low] < b[high]){
swap(a[low],b[high]);
low++;
high--;
}
else break;
}
int ans = 0;
for(int i = 0;i < n;i++)ans += a[i];
cout<<ans<<endl;
}
return 0;
}
C. Board Moves(数学)
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long
using namespace std;
const int N = 1000;
//小学生找规律都比你牛!!!!!!!!!!!!!!!!!!!!!!!!!!
int main(){
FAST;
rush(){
ll n;
ll ans = 0,term;
cin>>n;
for(ll i = 1;i <= (n - 1) / 2;i++)ans += i * i * 8;
cout<<ans<<endl;
}
return 0;
}
(补题)
D. Constructing the Array(优先队列)
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long
//80654425 May/19/2020 12:42UTC+8 answer_is_42 D - Constructing the Array GNU C++14 Accepted 109 ms 5400 KB
//借鉴自: https://www.cnblogs.com/streamazure/p/12897396.html
using namespace std;
const int N = 2e5 + 1000;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
//题解 每一次操作都会产生两个子序列 将这个子序列的头,尾和长度存入优先队列(按照长度优先重载)
struct node {
int l, r, len;
node(int a, int b, int c) {
l = a;
r = b;
len = c;
}
//优先队列从大到小排
//返回值为真意味着在队列排序中 this<a 成立
bool operator < (const node& a) const {
if (a.len == len) return l > a.l;
//如果长度相同而a的l较小(即靠近左边),则a>this(a更优先)
return len < a.len;
//否则如果a更长,则a>this(a更优先)
}
};
int ans[N];
int main(){
FAST;
rush(){
int n,count = 1;
cin>>n;
priority_queue<node>tro;
tro.push(node(1,n,n));
while(count <= n){
node x = tro.top();
tro.pop();
int place = (x.l + x.r) / 2;
ans[place] = count;
tro.push(node(x.l,place - 1,place - x.l));
tro.push(node(place + 1,x.r,x.r - place));
count++;
}
for(int i = 1;i <= n;i++)cout<<ans[i]<<" ";
cout<<endl;
}
return 0;
}
TO BE CONTINUED.....