8.2综合测试总结
T639114 大楼
题目描述
有
请判断是否存在比最左边第
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[1000007];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++)
if (a[i] > a[1]) {cout << i; return 0;}
cout << -1;
return 0;
}
T639104 信奥大联赛
题目描述
有
信奥大联赛 总决赛 按照以下步骤进行:
- 按照用户名的字典序,依次为每位用户分配编号
0, 1, \dots, N-1 。 -
请输出获胜者的用户名。
什么是字典序?字典序简单来说就是“单词在字典中出现的顺序”。更严格地说,对于由小写英文字母组成的两个不同字符串
下面用
- 设
L 为S, T 中较短的字符串的长度。对于i=1,2,\dots,L ,依次比较S_i 和T_i 是否相等。 - 如果存在
S_i \neq T_i 的i ,取最小的这样的i ,记为j 。比较S_j 和T_j ,若S_j 的字母顺序小于T_j ,则S < T ,否则S > T ,算法结束。 - 如果所有
S_i = T_i ,则比较S 和T 的长度,若S 比T 短,则S < T ,否则S > T ,算法结束。
输入格式
第一行输入一个整数
接下来
输出格式
输出答案
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, sum;
struct node {
string s;
int fen;
}a[500007];
bool cmp(const node &x, const node &y) {
return x.s < y.s;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].s >> a[i].fen, sum += a[i].fen;
sort(a, a + n, cmp);
cout << a[sum % n].s;
return 0;
}
T638475 调酒大师
题目描述
小a正在店里售卖鸡尾酒,他现在手里有
一份果汁白酒中必须包含以下两种条件:\
• 果汁和白酒加起来一共
现在请告诉小
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[1000007];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t, n, m;
cin >> t;
while (t--) {
cin >> n >> m;
int x = min(n, m), s = n + m;
int sum = s / 4;
cout << min(sum, x) << endl;
}
return 0;
}
T638471 小梦的su7
题目描述
小梦买了一辆
现在小梦想试试
因为小梦想来一场不回头的旅行,所以我们规定只能前进不能后退,小梦想请你帮他算算,最多能开车走多远。
输入格式
第一行包含两个整数
第二行包含
接下来的m行每行包含两个整数
保证
输出格式
输出一个整数,表示车辆可以行驶的最远距离。
说明/提示
数据范围
| 数据点 | 数据范围 | 特殊情况 |
|---|---|---|
| 无 | ||
| 无 | ||
| 无 |
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, sum, now;
struct node {
int x, t;
}a[100007];
int w[100007];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> w[i];
sum += w[i];
}
for (int i = 1; i <= m; i++) {
cin >> a[i].x >> a[i].t;
q.push({a[i].x, a[i].t});
}
for (int i = 1; i <= m + 1; i++) {
if (q.top().first - now > sum || q.empty()) {cout << now + sum; return 0;}
else {
int first = q.top().first;
int second = q.top().second;
if (first - now > w[second]) sum = sum - (first - now) + w[second];
now = first;
q.pop();
}
}
return 0;
}
错误原因
思路错误
正确思路
-
我们可以运用到贪心的策略 : 尽量用接下来要充的电池
-
我们既然已经有了贪心的策略, 那我们就可以先试着想一下暴力
不难想到, 复杂度为
O(n ^ 2) 的做法为 : 从充电站j 走向j + 1 的时候, 先用能最先加到电的电池但是如果这个电池用完了呢? 用下下个先加到电的电池
如果这个也用完了?继续找?。
-
我们可以考虑加速这个选择的过程, 不断选择最近可以充电的电池
用完之后, 把这个电池删掉, 插入这个电池的下一个充电站位置, 可以用最小堆(优先队列), 进行优化。
时间复杂度为
O(nlog(n)) -
我们可以定义四个数组:
-
-
还有一个优先队列与一个队列:
-
-
核心代码:
for (int i = 1; i <= n; i++) { cin >> a[i]; r[i] = a[i]; // 保存电池容量上限 } for (int i = 1; i <= m; i++) { cin >> x[i] >> t[i]; // 保证x从小到大 s[t[i]].push(x[i]); // 依次存储第t[i]块电池的充电站位置(依次升序) } for (int i = 1; i <= n; i++) s[i].push(1e18); //给所有电池都设置一个极大位置的充电站 for (int i = 1; i <= n; i++) { int first = s[i].front(); // 第i块电池目前最近的充电站 s[i].pop(); q.push({first, i}); // 将第i块电池最近充电站位置和i打包丢进优先队列。 // 优先队列按位置降序,显然里面应该存储的是每块电池及最近的充电站位置 } int ans = 0; for (int i = 1; i <= m; i++) { int len = x[i] - x[i - 1]; //当前充电站与上一站的距离 while (len > 0) { //按照优先队列的顺序用电,优先使用离充电站近的电池 if (q.empty()) break; pair<int, int> top = q.top(); q.pop(); if (a[top.second] <= len) { len -= a[top.second]; a[top.second] = 0; } else { a[top.second] -= len; len = 0; q.push(top); break; } } if (len > 0) {//如果电量不足以到达充电站 ans += (x[i] - x[i - 1] - len); break; } else { // 到达第i个充电站,给t[i]电池充满 a[t[i]] = r[t[i]]; // 充满 int fi = s[t[i]].front(); // 下一个充电站位置 s[t[i]].pop(); if(q.top().second == t[i]) q.pop(); // 先确定堆顶是t[i],再出队 q.push({fi, t[i]}); // 更新 ans += x[i] - x[i - 1]; } } for (int i = 1; i <= n; i++) ans += a[i]; //如果有剩余电量,增加到ans中 cout << ans << endl;
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
/*
贪心:尽量用接下来要充的电池
暴力:n ^ 2, 从充电站j走向j + 1的时候, 一定是先用能先加到的电池, 但是如果这个电池用完了呢? 用下下个先加到电的电池
如果这个也用完了?继续找?。
优化:考虑加速这个选择的过程, 不断选择最近可以充电的电池, 用完之后, 把这个电池删掉, 插入这个电池的下一个充电站位置,
可以用最小堆(优先队列), 进行优化。时间复杂度为O(nlog(n))
*/
const int N = 2e5 + 5;
int a[N], x[N], t[N];
int r[N]; // 每块电池的充电上限
signed main() {
int tt;
cin >> tt;
while (tt--) {
int n, m;
cin >> n >> m;
queue<int> s[n + 1]; //每个电瓶用一个队列存储优先级,即充电站位置
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;// 按照pair的第一个键值升序排列的最小堆
for (int i = 1; i <= n; i++) {
cin >> a[i];
r[i] = a[i]; // 保存电池容量上限
}
for (int i = 1; i <= m; i++) {
cin >> x[i] >> t[i]; // 保证x从小到大
s[t[i]].push(x[i]); // 依次存储第t[i]块电池的充电站位置(依次升序)
}
for (int i = 1; i <= n; i++) s[i].push(1e18); //给所有电池都设置一个极大位置的充电站
for (int i = 1; i <= n; i++) {
int first = s[i].front(); // 第i块电池目前最近的充电站
s[i].pop();
q.push({first, i}); // 将第i块电池最近充电站位置和i打包丢进优先队列。
// 优先队列按位置降序,显然里面应该存储的是每块电池及最近的充电站位置
}
int ans = 0;
for (int i = 1; i <= m; i++) {
int len = x[i] - x[i - 1]; //当前充电站与上一站的距离
while (len > 0) { //按照优先队列的顺序用电,优先使用离充电站近的电池
if (q.empty()) break;
pair<int, int> top = q.top();
q.pop();
if (a[top.second] <= len) {
len -= a[top.second];
a[top.second] = 0;
} else {
a[top.second] -= len;
len = 0;
q.push(top);
break;
}
}
if (len > 0) {//如果电量不足以到达充电站
ans += (x[i] - x[i - 1] - len);
break;
} else { // 到达第i个充电站,给t[i]电池充满
a[t[i]] = r[t[i]]; // 充满
int fi = s[t[i]].front(); // 下一个充电站位置
s[t[i]].pop();
if(q.top().second == t[i]) q.pop(); // 先确定堆顶是t[i],再出队
q.push({fi, t[i]}); // 更新
ans += x[i] - x[i - 1];
}
}
for (int i = 1; i <= n; i++) ans += a[i]; //如果有剩余电量,增加到ans中
cout << ans << endl;
}
return 0;
}