维护除了自己外的最大值

AcWing 5132. 奶牛照相

对于求除了当前点外其他点的最大值,

  • 1.笨拙的方法是维护最大值和次大值以及他们所对应的坐标,用pair可以实现。
  • 2.巧妙的办法是用前缀数组和后缀数组预处理

1的实现

#include <bits/stdc++.h> using namespace std; # define int long long typedef long long ll; typedef unsigned long long ull; typedef pair<int,int>pii; const int N = 2e5 + 10; const int M = 2e5 + 10; const int inf = 0x3f3f3f3f; const int mod = 998244353; int n, m; int ans[N]; int h[N],w[N]; vector<pii>v; bool cmp(pii d,pii e){ return d.first>e.first; } signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int t; t=1; while (t--) { cin>>n; int s=0; for(int i=1;i<=n;i++){cin>>w[i]>>h[i]; s+=w[i]; v.push_back({h[i],i}); } sort(v.begin(),v.end(),cmp); for(int i=1;i<=n;i++){ if(h[i]==v[0].first&&i==v[0].second){ ans[i]=v[1].first; } else ans[i]=v[0].first; } for(int i=1;i<=n;i++){ cout<<(ll)ans[i]*(s-w[i])<<" "; } } return 0; }
  • 方法2
#include <bits/stdc++.h> using namespace std; # define int long long typedef long long ll; typedef unsigned long long ull; typedef pair<int,int>pii; const int N = 2e5 + 10; const int M = 2e5 + 10; const int inf = 0x3f3f3f3f; const int mod = 998244353; int n, m; int ans[N]; int h[N],w[N]; int l[N],r[N]; bool cmp(pii d,pii e){ return d.first>e.first; } signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int t; t=1; while (t--) { cin>>n; int s=0; for(int i=1;i<=n;i++){cin>>w[i]>>h[i]; s+=w[i]; l[i]=max(h[i],l[i-1]); } for(int i=n;i>=1;i--)r[i]=max(h[i],r[i+1]); for(int i=1;i<=n;i++){ int ans=max(l[i-1],r[i+1]); cout<<(ll)ans*(s-w[i])<<" "; } } return 0; }

__EOF__

本文作者爱飞鱼
本文链接https://www.cnblogs.com/mathiter/p/17622039.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   potential-star  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示