Algorithm-Renewed
This commit is contained in:
32
Algorithm/Greedy/P1223 排队接水.cpp
Normal file
32
Algorithm/Greedy/P1223 排队接水.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <iomanip>
|
||||
#include <set>
|
||||
using namespace std;
|
||||
|
||||
struct cmp{
|
||||
bool operator()(const pair<int, int>& a, const pair<int, int>& b) const{
|
||||
if(a.second != b.second) return a.second < b.second;
|
||||
else return a.first < b.first;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
set<pair<int, int>, cmp> s;
|
||||
int n;
|
||||
cin >> n;
|
||||
int tmp;
|
||||
double all = 0;
|
||||
for(int i = 0; i < n; i++){
|
||||
cin >> tmp;
|
||||
s.emplace(make_pair(i + 1, tmp));
|
||||
}
|
||||
auto x = s.begin();
|
||||
for(int i = 0; i < n; i++){
|
||||
cout << (*x).first << " ";
|
||||
all += (n - i - 1) * (*x).second;
|
||||
x++;
|
||||
}
|
||||
cout << endl;
|
||||
cout << fixed << setprecision(2) << all / n << endl;
|
||||
return 0;
|
||||
}
|
||||
46
Algorithm/Greedy/P2240 【深基12.例1】部分背包问题.cpp
Normal file
46
Algorithm/Greedy/P2240 【深基12.例1】部分背包问题.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
struct cmp {
|
||||
bool operator()(const tuple<double, double, double>& a,
|
||||
const tuple<double, double, double>& b) const {
|
||||
return get<2>(a) > get<2>(b); // <20><><EFBFBD>Լ۱Ƚ<DBB1><C8BD><EFBFBD>
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
double t;
|
||||
cin >> n >> t;
|
||||
|
||||
vector<tuple<double, double, double>> arr(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
double a, b;
|
||||
cin >> a >> b;
|
||||
arr[i] = make_tuple(a, b, b / a); // (<28><><EFBFBD><EFBFBD>, <20><>ֵ, <20><>λ<EFBFBD><CEBB>ֵ)
|
||||
}
|
||||
|
||||
sort(arr.begin(), arr.end(), cmp());
|
||||
|
||||
double value = 0;
|
||||
int p = 0;
|
||||
|
||||
while (t > 0 && p < n) {
|
||||
if (t >= get<0>(arr[p])) {
|
||||
value += get<1>(arr[p]); // ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ
|
||||
t -= get<0>(arr[p]); // <20>ȼ<EFBFBD><C8BC><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
|
||||
p++;
|
||||
} else {
|
||||
value += t * get<2>(arr[p]); // ȡһ<C8A1><D2BB><EFBFBD><EFBFBD>
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cout << fixed << setprecision(2) << value << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
0
Algorithm/Greedy/Package.cpp
Normal file
0
Algorithm/Greedy/Package.cpp
Normal file
23
Algorithm/Greedy/UVa10954AddAll.cpp
Normal file
23
Algorithm/Greedy/UVa10954AddAll.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
int main(){
|
||||
long long sum = 0;
|
||||
priority_queue<int, vector<int>, greater<int>> q;
|
||||
int n;
|
||||
cin >> n;
|
||||
for(int i = 0; i < n; i++){
|
||||
int tmp;
|
||||
cin >> tmp;
|
||||
q.push(tmp);
|
||||
}
|
||||
while(q.size() > 1){
|
||||
int a = q.top();q.pop();
|
||||
int b = q.top();q.pop();
|
||||
int tmp = a + b;
|
||||
sum += tmp;
|
||||
q.push(tmp);
|
||||
}
|
||||
cout << sum << endl;
|
||||
return 0;
|
||||
}
|
||||
58
Algorithm/Greedy/UVa714Copying-Books.cpp
Normal file
58
Algorithm/Greedy/UVa714Copying-Books.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>->
|
||||
//<2F><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD> mid = (low + high) / 2
|
||||
//̰<><CCB0><EFBFBD><EFBFBD>֤ <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7>鷽ʽ
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool check(const vector<int>& a, int target, int k) {
|
||||
int js = 1; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>дԱ
|
||||
int temp = 0; // <20><>ǰ<EFBFBD><C7B0>дԱ<D0B4><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
for (int i = 0; i < a.size(); i++) {
|
||||
if (a[i] > target) return false; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD> target<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (temp + a[i] > target) { // <20><><EFBFBD><EFBFBD> target<65><74><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>дԱ
|
||||
js++;
|
||||
temp = a[i];
|
||||
} else {
|
||||
temp += a[i];
|
||||
}
|
||||
}
|
||||
|
||||
return js <= k; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
vector<int> arr;
|
||||
arr.resize(n);
|
||||
|
||||
int max = 0;
|
||||
int sum = 0;
|
||||
for(int i = 0; i < n; i++){
|
||||
cin >> arr[i];
|
||||
if(max < arr[i]) max = arr[i];
|
||||
sum += arr[i];
|
||||
}
|
||||
|
||||
int left = max;
|
||||
int right = sum;
|
||||
int result = right;
|
||||
|
||||
while (left <= right) {
|
||||
int mid = left + (right - left) / 2;
|
||||
if (check(arr, mid, k)) {
|
||||
result = mid;
|
||||
right = mid - 1;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
|
||||
cout << result << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user