思維、貪心、綜合
排隊打水
這道題目不算難,但是不注意還是會出現很多錯誤,比如結構體的書寫。以及自定義結構體排序。還有這里做的優化,使用前綴和記錄打水的等待時間,但是這里很容易出錯的點在于等待時間是應該是記錄的前一個人的前綴和。因為它不需要等待自己打水的等待時間。
#include<bits/stdc++.h>
using namespace std;
int n;
struct node{int index;int t;
}a[1010];
int s[1010];
bool cmp(node a, node b){return a.t < b.t;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;for(int i = 1; i <= n; i++){cin >> a[i].t;a[i].index = i;}sort(a + 1, a + n + 1, cmp);double ans = 0;for(int i = 1; i <= n; i++){s[i] += s[i - 1] + a[i].t; //構建前綴和數組ans += s[i - 1];cout << a[i].index << " ";}cout << endl;ans /= n;cout << fixed << setprecision(2) << ans;return 0;
}
合并果子
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;priority_queue<ll, vector<ll>, greater<>> q;for(int i = 0; i < n; i++){int num;cin >> num;q.push(num);}ll ans = 0;while(q.size() >= 2){ll a = q.top(); q.pop();ll b = q.top(); q.pop();ans += a + b;q.push(a + b);}cout << ans;return 0;
}
平均分配
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct node{ll b;ll c;ll diff;
}pr[200001];
bool cmp(node f, node l){return f.diff > l.diff;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);int n;cin >> n;for(int i = 1; i <= 2*n; i++){cin >> pr[i].b;}for(int i = 1; i <= 2*n; i++){cin >> pr[i].c;pr[i].diff = pr[i].b - pr[i].c; // 計算差值,降序排列,前n個數就是b>c的,后n個用n即可}sort(pr + 1, pr + 2 * n + 1, cmp);ll ans = 0;for(int i = 1; i <= n; i++){ans += pr[i].b;}for(int i = n + 1; i <= 2*n; i++){ans += pr[i].c;}cout << ans;return 0;
}
夢境巡查
#include<bits/stdc++.h>
using namespace std;
int n;
int a[100010], w[100010], b[100010];
void get_min(int lost){int begin = 0;int cur = 0;for(int i = 0; i <= n; i++){if(cur < a[i]){ // 當前值小于a[i]begin += a[i] - cur; // 初始值加上a[i] - curcur = a[i];}cur -= a[i];if(i != lost - 1) cur += b[i + 1];}w[lost] = begin;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;for(int i = 0; i <= n; i++){cin >> a[i];}for(int i = 1; i <= n; i++){cin >> b[i];}for(int i = 1; i <= n; i++){get_min(i);cout << w[i] << " ";}return 0;
}
雙指針,高精度,離散化
消消樂
#include<bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(0);cin.tie(0);string str;cin >> str;int l = 0;int r = str.size() - 1;int ans = str.size();while(l < r){if(str[l] == 'A' && str[r] == 'B'){ans -= 2;l++;r--;}else if(str[l] == 'B') l++;else if(str[r] == 'A') r--;}cout << ans;return 0;
}
A+B(高精度)
#include<bits/stdc++.h>
using namespace std;
vector<int> add(vector<int> &a, vector<int> &b){vector<int> c;int t = 0;for(int i = 0; i < a.size() || i < b.size(); i++){if(i < a.size()) t += a[i];if(i < b.size()) t += b[i];c.push_back(t%10);t /= 10;}if(t) c.push_back(1);return c;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);vector<int> a, b;string sa, sb;cin >> sa >> sb;for(int i = sa.size() - 1; i >= 0; i--) a.push_back(sa[i] - '0');for(int i = sb.size() - 1; i >= 0; i--) b.push_back(sb[i] - '0');vector<int> c = add(a, b);for(int i = c.size() - 1; i >= 0; i--) cout << c[i];return 0;
}
區間和
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 300010;
int n, m;
int a[N], s[N];
vector<int> alls;
vector<PII> add, query;
int find(int x){int l = 0, r = alls.size() - 1;while(l < r){int mid = l + r >> 1;if(alls[mid] >= x) r = mid;else l = mid + 1;}return r + 1;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m;for(int i = 0; i < n; i++){int x, c;cin >> x >> c;add.push_back({x, c});alls.push_back(x);}for(int i = 0; i < m; i++){int l, r;cin >> l >> r;query.push_back({l, r});alls.push_back(l);alls.push_back(r);}sort(alls.begin(), alls.end());alls.erase(unique(alls.begin(), alls.end()), alls.end());for(auto item : add){int x = find(item.first);a[x] += item.second;}for(int i = 1; i <= alls.size(); i++) s[i] = s[i - 1] + a[i];for(auto item : query){int l = find(item.first), r = find(item.second);cout << s[r] - s[l - 1] <<endl;}return 0;
}