8.27_407_课堂总结
U593635 交换
思路
模拟
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int a[4];
cin >> a[1] >> a[2] >> a[3];
swap(a[1], a[2]);
swap(a[1], a[3]);
for(int i = 1; i <= 3; i++){
cout << a[i] << ' ';
}
return 0;
}
T643661 猜猜芭蕾舞剧将如何结束
思路
模拟
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
ll m, n, lp = 0, rp;
cin >> m >> n;
rp = m;
for(int i = 1; i <= n; i++){
ll x;
cin >> x;
if(x < 0){
lp = max((ll)0, lp + x);
rp = max((ll)0, rp + x);
}else{
lp = min(m, lp + x);
rp = min(m, rp + x);
}
}
if(lp == rp){
cout << lp;
}else{
cout << -1;
}
return 0;
}
U593646 落落的去的数学问题五
暴力
思路
BFS
代码
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
using namespace std;
int k;
string ans[200001];
void bfs(){
queue<string> q;
for(int i = 1; i <= 9; i++){
q.push(to_string(i));
}
int cnt = 9;
while(q.size()){
string f = q.front();
q.pop();
if(cnt > k){
return;
}
if(f[f.size() - 1] > '0'){
ans[++cnt] = f + (char)(f[f.size() - 1] - 1);
q.push(f + (char)(f[f.size() - 1] - 1));
}
ans[++cnt] = f + f[f.size() - 1];
q.push(f + f[f.size() - 1]);
if(f[f.size() - 1] < '9'){
ans[++cnt] = f + (char)(f[f.size() - 1] + 1);
q.push(f + (char)(f[f.size() - 1] + 1));
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> k;
if(k < 10){
cout << k;
return 0;
}
bfs();
cout << ans[k];
return 0;
}
U593645 操作2
时间不够了QAQ
思路
前缀和 + 二分(不开long long见祖宗)
代码
#include<bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;
int n, m;
int a[200001];
ll cnt[200001];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
stable_sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++){
cnt[i] = cnt[i - 1] + a[i];
}
for(int i = 1; i <= m; i++){
int x;
cin >> x;
ll ans = 0;
int l = lower_bound(a + 1, a + n + 1, x) - a - 1;
int sum = cnt[l] - cnt[0];
int tmp = x * l;
ans += abs(tmp - sum);
l = upper_bound(a + 1, a + n + 1, x) - a;
sum = cnt[n] - cnt[l - 1];
tmp = x * (n - l + 1);
ans += abs(tmp - sum);
cout << ans << "\n";
}
return 0;
}