当前位置:网站首页>Luogu questionnaire - greed
Luogu questionnaire - greed
2022-07-18 12:15:00 【Crayon gold QAQ】
Rogue list - greedy
milk
Portal

cpp #include <bits/stdc++.h> using namespace std; // https://www.luogu.com.cn/problem/P1208 struct node { int p; // Price int t; // Number of owned } man[100000]; bool cmp(node a, node b) { return a.p < b.p; } int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> man[i].p >> man[i].t; } sort(man + 1, man + 1 + m, cmp); long long ans = 0; int cnt = 0; while (n) { if (n > man[++cnt].t) { ans += man[cnt].p * man[cnt].t; n -= man[cnt].t; } else { ans += man[cnt].p * n; n = 0; } } cout << ans; return 0; }Jump !

#include <bits/stdc++.h> using namespace std; // greedy ( Look for the largest and smallest jumps in the remaining stones ) // https://www.luogu.com.cn/problem/P4995#submit int stone[100000]; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> stone[i]; } sort(stone + 1, stone + 1 + n); int rnode = n; int lnode = 0; // lnode The initial value is 1! Because at first on the ground bool flag = true; long long ans = 0; while (lnode != rnode) { if (flag) { ans += (stone[rnode] - stone[lnode]) * (stone[rnode] - stone[lnode]); lnode++; flag = false; } else { ans += (stone[rnode] - stone[lnode]) * (stone[rnode] - stone[lnode]); rnode--; flag = true; } } cout << ans; return 0; } ``` ### Souvenir group  [ Portal ](https://www.luogu.com.cn/problem/P1094) ```cpp #include <bits/stdc++.h> using namespace std; // https://www.luogu.com.cn/problem/P1094 int gift[100000]; int main() { int m, n, cnt; cin >> m >> n; for (int i = 1; i <= n; i++) { cin >> gift[i]; } sort(gift + 1, gift + 1 + n); int rnode = n; int lnode = 1; cnt = 0; while (lnode < rnode) { if (gift[lnode] + gift[rnode] <= m) { cnt++; lnode++, rnode--; } else { rnode--; cnt++; } } if (lnode == rnode) { cnt++; } cout << cnt; return 0; } ``` ### Three Kingdoms game    [ Portal ](https://www.luogu.com.cn/problem/P1199) ```cpp #include <bits/stdc++.h> using namespace std; int gen[10000][10000]; // greedy + Game theory // Choose the second largest one each time ! int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 1; i < n; i++) { for (int j = i + 1; j <= n; j++) { cin >> gen[i][j]; gen[j][i] = gen[i][j]; } } int ans = -1; for (int i = 1; i <= n; i++) { sort(gen[i] + 1, gen[i] + 1 + n, greater<int>()); ans = max(ans, gen[i][2]); } cout << 1 << endl; cout << ans << endl; return 0; } ``` ### salesman   [ Portal ](https://www.luogu.com.cn/problem/P2672) ```cpp #include <bits/stdc++.h> using namespace std; // https://www.luogu.com.cn/problem/P2672 int N; struct node { int dis; int tire; } a[100000]; int t[100000]; // t[i] On behalf of i Prefix of maximum fatigue value and int d[100000]; // q[i] On behalf of i The fatigue value spent in distance among users with the highest fatigue value int post[100000]; // post[i] Delegate pick i After users with the maximum fatigue value , The maximum value left bool cmp(node a, node b) { return a.tire > b.tire; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> N; for (int i = 1; i <= N; i++) { cin >> a[i].dis; } for (int i = 1; i <= N; i++) { cin >> a[i].tire; } sort(a + 1, a + 1 + N, cmp); for (int i = 1; i <= N; i++) { t[i] = t[i - 1] + a[i].tire; } for (int i = 1; i <= N; i++) { d[i] = max(d[i - 1], a[i].dis * 2); } for (int i = N; i >= 1; i--) { post[i] = max(post[i + 1], 2 * a[i].dis + a[i].tire); } for (int i = 1; i <= N; i++) { cout << max(t[i] + d[i], t[i - 1] + post[i]) << endl; } return 0; } ```
边栏推荐
- 【idea】idea添加vm options
- 279. 完全平方数
- 1523. Count the number of odd numbers within the interval
- 2022年成都/杭州/厦门/武汉产品经理认证招生简章(NPDP)
- 第50篇-某查查请求头参数分析【2022-07-14】
- Pule frog 4d5d dynamic cinema | VR space travel equipment | VR takes you to travel in space
- 长安链介绍-02
- 我的两周年创作纪念日
- Solve the "cannot find module 'path' or its corresponding type declarations." in TS
- MySQL original field to hump naming
猜你喜欢

【Jailhouse 文章】Bao: a modern lightweight embedded hypervisor(2020)

【Jailhouse 文章】Bao: A Lightweight Static Partitioning Hypervisor for Modern Multi-Core Embedded...

JVM调优实战(详细版)

Openeuler knowledge: repo

In three steps, I finished MySQL in one day, which made me win tmall offer smoothly

On July 16, 2022, the results of cdga/cdgp data governance certification examination came out!

洛谷题单-贪心

Résolution du format d'image

2022年成都/杭州/厦门/武汉产品经理认证招生简章(NPDP)

JupyterLab安装
随机推荐
GDB or delve debug Go program, check variable display < optimized out > solution
长安链介绍-01
Image format analysis
openEuler 知:ip addr 查不到 ip 的解决方法
Unity (c) method for obtaining the encoding format of files
Why is it said that big companies are not paradise? What pits are there?
In three steps, I finished MySQL in one day, which made me win tmall offer smoothly
openSource 知:嵌入式软件列表
HDOJ-2057(A + B Again)
自动推理的逻辑02-命题微积分
我的两周年创作纪念日
openEuler 知:管理策略
Differences among screenwidth, clientwidth, offsetwidth, and scrollwidth
Build Detailed explanation of gradle configuration file (incomplete)
openEuler 知:SIG
gradle项目中 build.gradle 配置文件详解(未完成)
三個步驟,一天就搞定了MySQL,讓我順利拿下了天猫offer
Flink基础记录补充
vue+mysql连接数据库实现登录注册
uniapp uni-popup change