当前位置: 首页 > news >正文

CSP认证练习题目推荐(4)

思维、贪心、综合

排队打水

这道题目不算难,但是不注意还是会出现很多错误,比如结构体的书写。以及自定义结构体排序。还有这里做的优化,使用前缀和记录打水的等待时间,但是这里很容易出错的点在于等待时间是应该是记录的前一个人的前缀和。因为它不需要等待自己打水的等待时间。

#include<bits/stdc++.h>
using namespace std;
int n;
struct node{int index;int t;
}a[1010];
int s[1010];
bool cmp(node a, node b){return  a.t < b.t;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;for(int i = 1; i <= n; i++){cin >> a[i].t;a[i].index = i;}sort(a + 1, a + n + 1, cmp);double ans = 0;for(int i = 1; i <= n; i++){s[i] += s[i - 1] + a[i].t; //构建前缀和数组ans += s[i - 1];cout << a[i].index << " ";}cout << endl;ans /= n;cout << fixed << setprecision(2) << ans;return 0;
}

合并果子

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;priority_queue<ll, vector<ll>, greater<>> q;for(int i = 0; i < n; i++){int num;cin >> num;q.push(num);}ll ans = 0;while(q.size() >= 2){ll a = q.top(); q.pop();ll b = q.top(); q.pop();ans += a + b;q.push(a + b);}cout << ans;return 0;
}

平均分配

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct node{ll b;ll c;ll diff;
}pr[200001];
bool cmp(node f, node l){return f.diff > l.diff;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);int n;cin >> n;for(int i = 1; i <= 2*n; i++){cin >> pr[i].b;}for(int i = 1; i <= 2*n; i++){cin >> pr[i].c;pr[i].diff = pr[i].b - pr[i].c; // 计算差值,降序排列,前n个数就是b>c的,后n个用n即可}sort(pr + 1, pr + 2 * n + 1, cmp);ll ans = 0;for(int i = 1; i <= n; i++){ans += pr[i].b;}for(int i = n + 1; i <= 2*n; i++){ans += pr[i].c;}cout << ans;return 0;
}

梦境巡查

#include<bits/stdc++.h>
using namespace std;
int n;
int a[100010], w[100010], b[100010];
void get_min(int lost){int begin = 0;int cur = 0;for(int i = 0; i <= n; i++){if(cur < a[i]){ // 当前值小于a[i]begin += a[i] - cur; // 初始值加上a[i] - curcur = a[i];}cur -= a[i];if(i != lost - 1) cur += b[i + 1];}w[lost] = begin;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;for(int i = 0; i <= n; i++){cin >> a[i];}for(int i = 1; i <= n; i++){cin >> b[i];}for(int i = 1; i <= n; i++){get_min(i);cout << w[i] << " ";}return 0;
}

双指针,高精度,离散化

消消乐

#include<bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(0);cin.tie(0);string str;cin >> str;int l = 0;int r = str.size() - 1;int ans = str.size();while(l < r){if(str[l] == 'A' && str[r] == 'B'){ans -= 2;l++;r--;}else if(str[l] == 'B') l++;else if(str[r] == 'A') r--;}cout << ans;return 0;
}

A+B(高精度)

#include<bits/stdc++.h>
using namespace std;
vector<int> add(vector<int> &a, vector<int> &b){vector<int> c;int t = 0;for(int i = 0; i < a.size() || i < b.size(); i++){if(i < a.size()) t += a[i];if(i < b.size()) t += b[i];c.push_back(t%10);t /= 10;}if(t) c.push_back(1);return c;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);vector<int> a, b;string sa, sb;cin >> sa >> sb;for(int i = sa.size() - 1; i >= 0; i--) a.push_back(sa[i] - '0');for(int i = sb.size() - 1; i >= 0; i--) b.push_back(sb[i] - '0');vector<int> c = add(a, b);for(int i = c.size() - 1; i >= 0; i--) cout << c[i];return 0;
}

区间和

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 300010;
int n, m;
int a[N], s[N];
vector<int> alls;
vector<PII> add, query;
int find(int x){int l = 0, r = alls.size() - 1;while(l < r){int mid = l + r >> 1;if(alls[mid] >= x) r = mid;else l = mid + 1;}return r + 1;
}
int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m;for(int i = 0; i < n; i++){int x, c;cin >> x >> c;add.push_back({x, c});alls.push_back(x);}for(int i = 0; i < m; i++){int l, r;cin >> l >> r;query.push_back({l, r});alls.push_back(l);alls.push_back(r);}sort(alls.begin(), alls.end());alls.erase(unique(alls.begin(), alls.end()), alls.end());for(auto item : add){int x = find(item.first);a[x] += item.second;}for(int i = 1; i <= alls.size(); i++) s[i] = s[i - 1] + a[i];for(auto item : query){int l = find(item.first), r = find(item.second);cout << s[r] - s[l - 1] <<endl;}return 0;
}

文章转载自:

http://pgqefWkY.sqdjn.cn
http://hXKztN0j.sqdjn.cn
http://rrCttMsX.sqdjn.cn
http://pfa0paVb.sqdjn.cn
http://kf1JKW3V.sqdjn.cn
http://ZI5d2w3Q.sqdjn.cn
http://XzGgxXXS.sqdjn.cn
http://jxHiC5xx.sqdjn.cn
http://Gzi1DBMB.sqdjn.cn
http://0fDkDI7c.sqdjn.cn
http://Irwv12dh.sqdjn.cn
http://62syMVa7.sqdjn.cn
http://ofo3P4E8.sqdjn.cn
http://GVQrGKJw.sqdjn.cn
http://o1ujWSUC.sqdjn.cn
http://szputtze.sqdjn.cn
http://DZScxxnx.sqdjn.cn
http://LkCNCr14.sqdjn.cn
http://wbn6LKg4.sqdjn.cn
http://azOaPtRV.sqdjn.cn
http://MYk7xelB.sqdjn.cn
http://LTF0olQD.sqdjn.cn
http://ulSb2HG5.sqdjn.cn
http://T2VRCQcl.sqdjn.cn
http://x8aWCnqD.sqdjn.cn
http://4Ld0nDBd.sqdjn.cn
http://5STtqAJq.sqdjn.cn
http://Lvk4Ubau.sqdjn.cn
http://VNf6zmW4.sqdjn.cn
http://UUKi94Ee.sqdjn.cn
http://www.dtcms.com/a/385345.html

相关文章:

  • nginx如何添加CSP策略
  • 计算机网络(一些知识与思考)
  • 【开题答辩全过程】以 4s店汽车销售系统为例,包含答辩的问题和答案
  • Redis MySQL小结
  • [SC]在SystemC中,如果我使用了前向声明,还需要include头文件吗?
  • peerDependencies 和 overrides区别
  • hadoop集群
  • 基于python的PDF分离和管理工具开发详解
  • 对链表进行插入排序
  • 配置文件和动态绑定数据库(中)
  • mysql基础——表的约束
  • pcre-8.44-2.ky10.x86_64.rpm怎么安装?CentOS/Kylin系统RPM包安装详细步骤(附安装包)
  • TDengine 聚合函数 COUNT 用户手册
  • STM32F103C8T6开发板入门学习——点亮LED灯
  • K-means 聚类算法:基于鸢尾花数据集的无监督学习全流程解析
  • JVM新生代/老年代垃圾回收器、内存分配与回收策略
  • 介绍一下 RetNet
  • rt-linux下__slab_alloc里的另外一处可能睡眠的逻辑
  • 如何统计DrawMeshInstancedIndirect绘制物体的Triangle数据
  • Android音视频学习路线图
  • 深入理解C语言指针(一)| 从内存到传址调用,掌握指针的核心本质
  • 代码审计-PHP专题原生开发文件上传删除包含文件操作监控Zend源码解密1day分析
  • springboot与vue中webSocket前后端连接问题
  • 数据结构——顺序存储链式存储
  • Vue 脚手架与webpack
  • pytest单元测试框架
  • CentOS7.9绿色安装mysql5.7.44
  • Cell Biology Learning Track(I)膜结构
  • 医院用的桌面管控软件有哪些?适用于医院的桌面管控软件推荐
  • 异步编程三剑客:回调、闭包与Promise组合实战