欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 牛客月赛115 C题-命运之弹 题解

牛客月赛115 C题-命运之弹 题解

2025/5/5 9:32:26 来源:https://blog.csdn.net/qq_45554473/article/details/147702929  浏览:    关键词:牛客月赛115 C题-命运之弹 题解

原题链接

https://ac.nowcoder.com/acm/contest/107879/C

题目描述

在这里插入图片描述

解题思路

记录每个数字出现的次数。枚举使用「转瞬即逝」的位置,统计后边比当前数字更大的数的数量,进而统计、更新答案。
详细细节见代码,代码里有详细的注释解释。

代码(CPP)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define endl "\n"
const int maxn = 2e5 + 10;
const int INF = 1e9;
int a[maxn], num[maxn], n;void solve() {cin >> n;for (int i = 1; i <= n; i++) {cin >> a[i];}int q;cin >> q;int v;      // 初始幸运值cin >> v;// 计数for (int i = 1; i <= n; i++) {num[a[i]]++;}// 枚举使用「转瞬即逝」的位置,统计后边比当前数字更大的数的数量即可int ans = INF;int cnt = 0;    // 统计前面大于v的数的个数for (int i = 1; i <= n; i++) {num[a[i]]--;// 统计后边比当前数字a[i]更大的数的数量即可int sum = 0;for (int j = a[i] + 1; j <= 100; j++) {sum += num[j];}// 统计答案,如果将当前数使用转瞬即逝,则本次代价为前面大于v的数的个数加上后边大于a[i]的数的数量ans = min(ans, sum + cnt);// 统计前面大于v的数的个数if (a[i] > v) cnt++;}cout << ans << endl;
}int main() {
//     freopen("in.txt", "r", stdin);ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout << fixed;cout.precision(18);solve();return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词