Algo-Trian

This commit is contained in:
e2hang
2025-09-21 12:21:50 +08:00
parent 24522486f1
commit 3bde00039c
25 changed files with 842 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
int sum = 0;
for(int i = 0; i < n; i++){
cin >> a[i];
sum += a[i];
}
int avg = sum / n;
int js = 0;
for(int i = 0; i < n - 1; i++){
if(a[i] != avg){
int tmp = a[i] - avg;
a[i] = avg;
a[i + 1] += tmp;
js++;
}
}
cout << js << endl;
return 0;
}

View File

@@ -0,0 +1,26 @@
#include <iostream>
#include <queue>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int>> q;
for(int i = 0; i < n; i++){
int a;
cin >> a;
q.emplace(a);
}
int value = 0;
int min1 = 0, min2 = 0;
while(q.size() > 1){
min1 = q.top(); q.pop();
min2 = q.top(); q.pop();
int tmp = min1 + min2;
value += tmp;
q.emplace(tmp);
}
cout << value<< endl;
return 0;
}

View File

@@ -0,0 +1,39 @@
#include <iostream>
#include <string>
#include <deque>
using namespace std;
int main() {
string s;
int k;
cin >> s >> k;
deque<char> q;
for(char curr : s) {
while(!q.empty() && k > 0 && q.back() > curr){
q.pop_back();
k--;
}
q.push_back(curr);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3> k <20><><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>ɾ<EFBFBD><C9BE>ջβ
while(k > 0 && !q.empty()) {
q.pop_back();
k--;
}
// ƴ<>ӽ<EFBFBD><D3BD><EFBFBD>
string res;
for(char c : q) res += c;
// ȥ<><C8A5>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
int i = 0;
while(i < res.size() && res[i] == '0') i++;
res = res.substr(i);
if(res.empty()) res = "0";
cout << res << endl;
return 0;
}