AcWing第84场周赛

本蒟蒻第一次AK周赛


第一题、最大数量

本题使用桶排序

代码

#include<bits/stdc++.h> using namespace std; int m[10005]; int main() { int n; cin>>n; for(int i=1;i<=n;i++){ int a,b; cin>>a>>b; m[(a*100)+b]++; } sort(m,m+2459); cout<<m[2458]; return 0; }

第二题、前缀和序列

前置知识:AcWing算法基础课——前缀和

代码

#include<bits/stdc++.h> #define int long long using namespace std; const int N = 100010; int a[N],q[N],b[N],q1[N]; signed main() { int n,m; cin>>n; q[0]=0; for(int i=1;i<=n;i++) {cin>>a[i];b[i]=a[i];} sort(b+1,b+1+n); cin>>m; for(int i=1;i<=n;i++) {q[i]=q[i-1]+a[i];q1[i]=q1[i-1]+b[i];} while(m--){ int s,l,r; cin>>s>>l>>r; if(s==1){ cout<<q[r]-q[l-1]<<endl; } else cout<<q1[r]-q1[l-1]<<endl; } }

第三题、买可乐

第一种方法、暴力

时间复杂度:$O(n)$

#include <iostream> #include <cstring> #include <algorithm> typedef long long LL; using namespace std; LL c, d, n, m, k; int main() { cin >> c >> d >> n >> m >> k; LL need = n * m - k; if (need <= 0) { cout << 0 << endl; return 0; } LL ans = 1e18; for (LL i = 0; i <= 10000; i ++ ) for (LL j = 0; j <= 10000; j ++ ) if (n * i + j >= need) ans = min(ans, c * i + d * j); cout << ans << endl; return 0; }

第二种方法、贪心

时间复杂度$O(1)$

#include <iostream> #include <cstring> #include <algorithm> using namespace std; int main() { int c, d, n, m, k; cin >> c >> d >> n >> m >> k; int cnt = n * m - k; if (cnt <= 0) puts("0"); else cout << min({cnt * d, cnt / n * c + cnt % n * d, (cnt + n - 1) / n * c}) << endl; return 0; }

__EOF__

本文作者yexc
本文链接https://www.cnblogs.com/LuoGuyexc/p/17018444.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   StudyingFaher  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示