https://vjudge.net/problem/CSES-1629
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define N 200005
#define int long long
using namespace std;
signed main(){
IOS
int n;
cin >> n;
pair<int, int> arr[N];
for(int i = 0;i < n;i++) cin >> arr[i].first >> arr[i].second;
sort(arr, arr + n, [&](pair<int, int> a, pair<int, int> b){return a.second < b.second;});
int ans = 1, pre = arr[0].second;
for(int i = 1;i < n;i++){
if(arr[i].first >= pre){
pre = arr[i].second;
ans++;
}
}
cout << ans << '\n';
return 0;
}
https://vjudge.net/problem/CSES-1640
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int n, x;
vector<pair<int, int>> arr;
int main(){
IOS
cin >> n >> x;
for(int i = 0;i < n;i++){
int num, idx;
cin >> num;
arr.push_back({num, i + 1});
}
sort(arr.begin(), arr.end());
// for(int i = 0;i < n;i++) cout << arr[i].first << ' ';
int ans_x, ans_y;
bool judge = false;
for(int l = 0, r = n - 1;l < n;l++){
while(l < r && arr[l].first + arr[r].first > x) r--;
if(l != r && arr[l].first + arr[r].first == x){
judge = true;
ans_x = arr[l].second;
ans_y = arr[r].second;
break;
}
}
if(judge) cout << ans_y << ' ' << ans_x << '\n';
else cout << "IMPOSSIBLE" << '\n';
return 0;
}
https://vjudge.net/problem/Kattis-grandpabernie
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
using namespace std;
int main(){
IOS
int n;
unordered_map<string, vector<int> > mp;
cin >> n;
vector<string> vec;
for(int i = 0;i < n;i++){
string str;
int tmp;
cin >> str >> tmp;
if(mp.count(str)) mp[str].push_back(tmp);
else{
vec.push_back(str);
mp.insert({str, vector<int>()});
mp[str].push_back(tmp);
}
}
int query;
cin >> query;
for(auto i : vec){
sort(mp[i].begin(), mp[i].end());
}
while(query--){
string str;
int tmp;
cin >> str >> tmp;
cout << mp[str][tmp - 1] << '\n';
}
return 0;
}
https://vjudge.net/problem/UVA-12338
待完成
https://vjudge.net/problem/CSES-1076
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
void balance(multiset<int, greater<int> > &left, multiset<int, less<int> > &right){
while(left.size() > right.size()){
right.insert(*left.begin());
left.erase(left.begin());
}
while(left.size() < right.size()){
left.insert(*right.begin());
right.erase(right.begin());
}
}
int main(){
IOS
int n, k, arr[200005];
cin >> n >> k;
for(int i = 0;i < n;i++) cin >> arr[i];
multiset<int, greater<int> > left;
multiset<int, less<int> > right;
for(int i = 0;i < n;i++){
if(!left.size() || arr[i] <= *left.begin()) left.insert(arr[i]);
else right.insert(arr[i]);
if(i >= k){
if(left.size() && arr[i - k] <= *left.begin())
left.erase(left.find(arr[i - k]));
else right.erase(right.find(arr[i - k]));
}
balance(left, right);
if(i >= k - 1) cout << *left.begin() << ' ';
}
cout << '\n';
return 0;
}
set | map | unordered map | |
---|---|---|---|
插入 | |||
查詢 | |||
插入 |
給定 n 場電影中的開始時間以及結束時間,要求最多可以看完幾場完整的電影?
pair<int, int> arr[N];
for(int i = 0;i < n;i++) cin >> arr[i].first >> arr[i].second;
sort(arr, arr + n, [&](pair<int, int> a, pair<int, int> b){return a.second < b.second;});
int ans = 1, pre = arr[0].second;
for(int i = 1;i < n;i++){
if(arr[i].first >= pre){
pre = arr[i].second;
ans++;
}
}
greedy 解,對各場電影的結束時間 sort ,接著用迴圈跑過一次,看各場電影的起頭是否超過上一場電影的結束時間。
給定 n 個整數求任兩個數字加起來等於 x 。
for(int l = 0, r = n - 1;l < n;l++){
while(l < r && arr[l].first + arr[r].first > x) r--;
if(l != r && arr[l].first + arr[r].first == x){
judge = true;
ans_x = arr[l].second;
ans_y = arr[r].second;
break;
}
}
雙指針解,藉由雙指針不斷逼近我們要的 x 。
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up