2020牛客国庆集训派对day3 Leftbest
Leftbest
鏈接:https://ac.nowcoder.com/acm/contest/7830/A
來源:??途W
題目描述
Jack is worried about being single for his whole life, so he begins to use a famous dating app. In this app, the user is shown single men/women’s photos one by one, and the user may choose between “yes” and “no”. Choosing “yes” means an invitation while choosing “no” means nothing. The photos would be shown one by one until the number of rest photos to be shown reaches zero. Of course, efficient and single Jack would always choose “yes”.
When viewing photos, Jack would have a “fake impression point” on every photo, which is not accurate. To calculate the “true impression point” of one photo, Jack would recall the “fake impression point” of every previous photo whose “fake impression point” is larger than this photo, and regard the smallest “fake impression point” of them as the “true impression point” of this photo. Jack would like to sum the “true impression point” of all photos as the outcome of his effort.
Note that if such a larger “fake impression point” does not exist, the “true impression point” of this photo is zero.
輸入描述:
The first line contains an integer {n}n (1 \le n \le 100,0001≤n≤100000) — the number of photos.
The second line contains n integers is the “fake impression point” of the i-th photo.
輸出描述:
Output a single integer — the sum of the “true impression point” of all photos.
示例1
輸入
復制
輸出
復制
題意:
排在a[i]前面 比a[i]大的數中最小數的和是多少?
題解:
第一反應是用大小堆來做,思路很簡單,但是卻超時了。。
后來想起來set里面本身就是函數是用來求這個的
所以直接
j=s.upper_bound(x)就行
STL有好多現成的東西,太久沒用都忘得差不多了
代碼:
一開始大小堆的代碼:
#include<cstdio> #include<iostream> #include<queue> using namespace std; const int maxn=1e5+8; int a[maxn]; typedef long long ll; priority_queue<int,vector<int>,less<int> >q;//從小到大 priority_queue<int,vector<int>,greater<int> >w;//從大到小 int main() {ios::sync_with_stdio(false);int n;scanf("%d",&n);ll sum=0;for(int i=1;i<=n;i++){int x;scanf("%d",&x);while(!q.empty()){// printf("q.top=%d\n",q.top());//cout<<"q.top="<<q.top<<endl;if(q.top()>x){w.push(q.top());q.pop();}else break;}if(!w.empty())sum+=w.top();while(!w.empty()){q.push(w.top());w.pop();} q.push(x);}cout<<sum;return 0; }AC代碼:
#include<cstdio> #include<iostream> #include<queue> #include<set> #include<algorithm> using namespace std; const int maxn=1e5+8; set<int>s; typedef long long ll; int main() {ios::sync_with_stdio(0);int n;cin>>n;ll sum=0;while(n--){int x;cin>>x;s.insert(x);auto j=s.upper_bound(x);if(j!=s.end())sum+=*j;}cout<<sum;return 0; }擴展
剛才那個題求的是前綴較大的最小值
我們擴展到其他幾個
注意:
set在內部會自動排序,且會自動查重(即每個數最多出現一次)
前綴較大的最大值
#include <iostream> #include <set> using namespace std; typedef long long ll; int main(){int n,x;ll ans=0;ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);set<int> s;cin>>n;cin>>x;s.insert(x);n--;while(n--){cin>>x;auto it=s.rbegin(); //不能"auto it=s.end()-1;"會報錯//auto it =s.end(); it--;if(x<*it) ans+=*it;s.insert(x);}cout<<ans<<endl;return 0; }前綴較小的最大值
#include <iostream> #include <set> using namespace std; typedef long long ll; int main(){int n,x;ll ans=0;ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);set<int> s;cin>>n;while(n--){cin>>x;s.insert(x);auto it=s.lower_bound(x);if(it!=s.begin() && it!=s.end()){ //兩邊都要考慮,begin是因為可能找不到,end是因為可能都比查找值小it--;ans+=*it;} }cout<<ans<<endl;return 0; }前綴較小的最小值
#include <iostream> #include <set> using namespace std; typedef long long ll; int main(){int n,x;ll ans=0;ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);set<int> s;cin>>n;cin>>x;s.insert(x);n--;while(n--){cin>>x;auto it=s.begin();if(x>*it){ans+=*it;}s.insert(x);}cout<<ans<<endl;return 0; }總結
以上是生活随笔為你收集整理的2020牛客国庆集训派对day3 Leftbest的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 记事本的 ANSI、Un
- 下一篇: 把我的心脏带回祖国教案 把我的心脏带回祖