链接:https://ac.nowcoder.com/acm/contest/338/H
来源:牛客网
the first line of input contains two integer n and k--the number of hamburgers on the counter and the number of plans Kuangyeye had;i
the next line contains n integer--the i-th integer represents the weight of i-th hamburger,namely w
;i
Each the following k line contains two integer a
and bi
,represents Kuangyeye‘s strategy in his i-th plan.
Output contain a single integer,represents maximum weight of hamburgers Kuangyeye can eat.
1≤n,k≤100000,1≤ai
≤bi
≤n,1≤wi
≤10000
题解:排序,计算前缀和,模拟
#include <iostream> #include <algorithm> using namespace std; int w[100050]; long long dp[100050]; bool cmp(const int& a,const int& b) { return a>b; } int main() { int n,k; long long ans=0; cin>>n>>k; for(int i=1;i<=n;i++) cin>>w[i]; sort(w+1,w+n+1,cmp); dp[0]=0; for(int i=1;i<=n;i++) { dp[i]=dp[i-1]+w[i]; } while(k--) { int l,r; cin>>l>>r; ans=max(dp[r]-dp[l-1],ans); } cout<<ans<<endl; return 0; }
原文:https://www.cnblogs.com/DWVictor/p/10229893.html