日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2020牛客国庆集训派对day3 Leftbest

發布時間:2023/12/3 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2020牛客国庆集训派对day3 Leftbest 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Leftbest
鏈接:https://ac.nowcoder.com/acm/contest/7830/A
來源:牛客網

題目描述

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
輸入
復制

4 2 1 4 3

輸出
復制

6

題意:

排在a[i]前面 比a[i]大的數中最小數的和是多少?

題解:

第一反應是用大小堆來做,思路很簡單,但是卻超時了。。
后來想起來set里面本身就是函數是用來求這個的

lower_bound(val); //查找大于等于val第一個元素的位置,沒有找到返回set::end()upper_bound(val); //查找大于val第一個元素的位置,沒有找到返回set::end()

所以直接
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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。