Algo-Trian
This commit is contained in:
26
Algorithm/Greedy/P1031 [NOIP 2002 提高组] 均分纸牌.cpp
Normal file
26
Algorithm/Greedy/P1031 [NOIP 2002 提高组] 均分纸牌.cpp
Normal 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;
|
||||
}
|
||||
26
Algorithm/Greedy/P1090 [NOIP 2004 提高组] 合并果子.cpp
Normal file
26
Algorithm/Greedy/P1090 [NOIP 2004 提高组] 合并果子.cpp
Normal 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;
|
||||
}
|
||||
39
Algorithm/Greedy/P1106 删数问题.cpp
Normal file
39
Algorithm/Greedy/P1106 删数问题.cpp
Normal 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;
|
||||
}
|
||||
|
||||
0
Algorithm/Greedy/一定要找对贪心策略
Normal file
0
Algorithm/Greedy/一定要找对贪心策略
Normal file
Reference in New Issue
Block a user