【基礎演算法】多路歸併題集
阿新 • • 發佈:2022-05-17
62. 醜數
class Solution { public: int getUglyNumber(int n) { vector<int> q(1, 1); int i = 0, j = 0, k = 0; while( -- n) // 迴圈 n - 1 次 { int t = min(q[i] * 2, min(q[j] * 3, q[k] * 5)); q.push_back(t); if(t == q[i] * 2) i ++ ; if(t == q[j] * 3) j ++ ; if(t == q[k] * 5) k ++ ; } return q.back(); } };
1262. 魚塘釣魚
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 110; int n, T; int a[N], d[N], l[N], spend[N]; int get(int k) { return max(0, a[k] - d[k] * spend[k]); } int work(int n, int T) { int res = 0; memset(spend, 0, sizeof spend); for(int i = 0; i < T; i ++ ) { int t = 1; for(int j = 1; j <= n; j ++ ) if(get(t) < get(j)) t = j; res += get(t); spend[t] ++ ; } return res; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i ++ ) scanf("%d", &a[i]); for(int i = 1; i <= n; i ++ ) scanf("%d", &d[i]); for(int i = 2; i <= n; i ++ ) { scanf("%d", &l[i]); l[i] += l[i - 1]; } scanf("%d", &T); int res = 0; for(int i = 1; i <= n; i ++ ) res = max(res, work(i, T - l[i])); printf("%d\n", res); return 0; } 作者:NFYD 連結:https://www.acwing.com/activity/content/code/content/3475286/ 來源:AcWing 著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
1541. 世界首富
#include <iostream> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 210; int n, m; struct Person { string name; int age, w; bool operator< (const Person& t) const { if(w != t.w) return w > t.w; if(age != t.age) return age < t.age; return name < t.name; } }; vector<Person> ages[N]; int idx[N]; int main() { scanf("%d%d", &n, &m); char name[10]; for(int i = 0; i < n; i ++ ) { int age, w; scanf("%s%d%d", name, &age, &w); ages[age].push_back({name, age, w}); } for(auto& c : ages) sort(c.begin(), c.end()); for(int T = 1; T <= m; T ++ ) { printf("Case #%d:\n", T); int cnt, a, b; scanf("%d%d%d", &cnt, &a, &b); memset(idx, 0, sizeof idx); bool exists = false; while(cnt -- ) { int t = -1; for(int i = a; i <= b; i ++ ) { if(idx[i] < ages[i].size()) { if(t == -1 || ages[i][idx[i]] < ages[t][idx[t]]) t = i; } } if(t == -1) break; auto& p = ages[t][idx[t]]; printf("%s %d %d\n", p.name.c_str(), p.age, p.w); idx[t] ++ ; exists = true; } if(!exists) puts("None"); } return 0; } 作者:NFYD 連結:https://www.acwing.com/activity/content/code/content/1790990/ 來源:AcWing 著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
3280. 推薦系統
#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <vector>
#include <set>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> PII;
const int N = 55;
int m, n;
set<PII> g[N];
unordered_map<int, int> f[N];
int sum[N], cnt[N];
set<PII>::reverse_iterator it[N];
vector<int> ans[N];
int main()
{
scanf("%d%d", &m, &n);
for(int i = 1; i <= n; i ++ )
{
int id, score;
scanf("%d%d", &id, &score);
for(int j = 0; j < m; j ++ )
{
f[j][id] = score;
g[j].insert({score, -id});
}
}
int Q;
scanf("%d", &Q);
while(Q -- )
{
int t;
scanf("%d", &t);
if(t == 1)
{
int type, id, score;
scanf("%d%d%d", &type, &id, &score);
f[type][id] = score;
g[type].insert({score, -id});
}
else if(t == 2)
{
int type, id;
scanf("%d%d", &type, &id);
g[type].erase({f[type][id], -id});
f[type].erase(id);
}
else
{
int tot;
scanf("%d", &tot);
for(int i = 0; i < m; i ++ )
{
scanf("%d", &sum[i]);
it[i] = g[i].rbegin();
cnt[i] = 0;
ans[i].clear();
}
while(tot -- )
{
int k = -1;
for(int i = 0; i < m; i ++ )
{
if(it[i] != g[i].rend() && cnt[i] < sum[i])
{
if(k == -1 || it[i]->fi > it[k]->fi)
{
k = i;
}
}
}
if(k == -1) break;
ans[k].push_back(-it[k]->se);
cnt[k] ++ ;
it[k] ++ ;
}
for(int i = 0; i < m; i ++ )
{
if(ans[i].empty()) puts("-1");
else
{
for(auto x : ans[i])
{
printf("%d ", x);
}
puts("");
}
}
}
}
return 0;
}
作者:NFYD
連結:https://www.acwing.com/activity/content/code/content/1742801/
來源:AcWing
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
3874. 三元組的最小距離
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int l, m, n;
int a[N], b[N], c[N];
int main()
{
scanf("%d%d%d", &l, &m, &n);
for(int i = 0; i < l; i ++ ) scanf("%d", &a[i]);
for(int i = 0; i < m; i ++ ) scanf("%d", &b[i]);
for(int i = 0; i < n; i ++ ) scanf("%d", &c[i]);
LL res = 1e18;
for(int i = 0, j = 0, k = 0; i < l && j < m && k < n; )
{
int x = a[i], y = b[j], z = c[k];
res = min(res, (LL)max(max(x, y), z) - (LL)min(min(x, y), z));
if(x <= y && x <= z) i ++ ;
else if(y <= x && y <= z) j ++ ;
else k ++ ;
}
printf("%lld\n", res * 2);
return 0;
}
作者:NFYD
連結:https://www.acwing.com/activity/content/code/content/2883052/
來源:AcWing
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。