江苏建设人才网网站,圣玺企业网站建设,坂田的做网站公司,企业网络营销策划论文目录 1 基础知识2 模板3 工程化 1 基础知识
暂无。。。
2 模板
暂无。。。
3 工程化
题目1#xff1a;排队打水。给定N个数#xff0c;表示装满每个桶所需的时间#xff0c;请你安排打水顺序#xff0c;使得等待时间最小#xff0c;并输出该最小值。
解题思路#… 目录 1 基础知识2 模板3 工程化 1 基础知识
暂无。。。
2 模板
暂无。。。
3 工程化
题目1排队打水。给定N个数表示装满每个桶所需的时间请你安排打水顺序使得等待时间最小并输出该最小值。
解题思路将所需时间小的桶排在前面。
C代码如下
#include iostream
#include vector
#include algorithmusing namespace std;int main() {int n;cin n;vectorint nums;for (int i 0; i n; i) {int x;cin x;nums.emplace_back(x);}sort(nums.begin(), nums.end());long long res 0;for (int i 0; i n; i) {res nums[i] * (n - i - 1);}cout res endl;return 0;
}题目2货仓选址问题。给定N个数求一个数x使得这N个数减去x的绝对值之和最小输出这个最小值。
解题思路贪心做法。当n为奇数时x取中位数当n为偶数时x取这两个中位数之间包含这两个中位数的任意一个数即可。
C代码如下
#include iostream
#include algorithm
#include vectorusing namespace std;int main() {int n; cin n;vectorint nums(n);for (int i 0; i n; i) cin nums[i];sort(nums.begin(), nums.end());long long res 0;for (int i 0; i n; i) res abs(nums[i] - nums[n/2]);cout res endl;return 0;
}题目3耍杂技的牛。有N头牛它有重量和强壮值两个属性将它们堆叠起来某头牛的风险值等于其上所有牛的重量减去自身的强壮值。N头牛总共有N个风险值有一个最大风险值。不同的排序方式会有不同的最大风险值求其最小值。
解题思路按照重量和强壮值之和从小到大排序这是最优的排序方式会获得最小的最大风险值。
C代码如下
#include iostream
#include algorithm
#include vectorusing namespace std;int main() {int n;cin n;vectorpairint,int nums;for (int i 0; i n; i) {int w, s;cin w s;nums.emplace_back(w,s);}sort(nums.begin(), nums.end(), [](const pairint,int a, const pairint,int b) {return a.first a.second b.first b.second; });long long res -2e9;long long sum 0;for (auto [w, s] : nums) {res max(res, sum - s);sum w;}cout res endl;return 0;
}