8.2/8.3课堂自我总结
lx01220122 · · 个人记录
1.课堂笔记
二分答案 系统内部STL
lower_bound/upper_bound
lower_bound(数组名+起点下标,数组名+终点下标+1,x)-数组名!
找到数组范围内,第一个大于等于x的元素的地址-数组名=真实的下标
upper_bound(数组名+起点下标,数组名+终点下标+1,x)-数组名!
找到数组范围内,第一个大于x的元素的地址-数组名=真实的下标
1.需要传递起点和终点
2.数组需要从小到大排序
3.找不到这个元素,答案是终点+1
2. 题目总结
P1318 积水面积
思路
本题的数据非常的水,所以我们有三种做法;
1.一行一行暴力枚举
定义两个int类型的数组maxl,maxr一个记录从1到i中最大的a[j],另一个记录从i+1到n中最大的a[j],再打擂台找出maxl和maxr中最小的数,最后进行计算算出这一区块的积水面积;
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
int n;
int a[N];
int ans=0;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
int maxr=-1;
int maxl=-1;
for(int j=1;j<=i-1;j++){
if(maxl<a[j]){
maxl=a[j];
}
}
for(int j=i+1;j<=n;j++){
if(maxr<a[j]){
maxr=a[j];
}
}
if(a[i]<min(maxl,maxr))
ans+=min(maxl,maxr)-a[i];
}
cout<<ans;
return 0;
}
2.一列一列暴力枚举
定义一个bool类型的变量f判断是否出现第一堵墙,int类型变量wt记录当前区块的积水面积,然后在出现比i更高 的墙时让ans统计总的积水面积;
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
int n;
int a[N];
int ans=0;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=5000;i++){
int wt=0;
bool f=0;
for(int j=1;j<=n;j++){
if(a[j]>=i&&f==1){
ans+=wt;
wt=0;
}
if(a[j]>=i){
f=1;
}
if(f==1&&a[j]<i){
wt++;
}
}
}
cout<<ans;
return 0;
}
3.前缀和
定义数组pre,suf,正反向记录前缀和,然后用sum统计总的积水面积;
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
ll n;
ll a[N];
ll pre[N];
ll suf[N];
ll ans=0;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
pre[i]=max(pre[i-1],a[i]);
}
for(int i=n;i>=1;i--){
suf[i]=max(suf[i+1],a[i]);
}
for(int i=1;i<=n;i++){
ans+=min(pre[i],suf[i])-a[i];
}
cout<<ans;
return 0;
}
P2697 宝石串
思路
定义一个sum如果宝石是红的sum[i]=sum[i-1]-1;如果宝石是绿的sum[i]=sum[i-1]+1;然后定义两个map,pre,suf 记录相同的sum[i],最后打擂台计算出答案;
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
string s;
int sum[N];
map<int,int> pre;
map<int,int> suf;
int main(){
cin>>s;
int maxx=0;
int n=s.size();
s="#"+s;
for(int i=1;i<=n;i++){
if(s[i]=='G')
sum[i]=sum[i-1]-1;
if(s[i]=='R')
sum[i]=sum[i-1]+1;
pre[sum[i]]=-1;
}
pre[0]=0;
for(int i=1;i<=n;i++){
if(pre[sum[i]]==-1){
pre[sum[i]]=i;
}
suf[sum[i]]=i;
}
for(int i=0;i<=n;i++){
int l=pre[sum[i]]+1;
int r=suf[sum[i]];
maxx=max(maxx,r-l+1);
}
cout<<maxx;
return 0;
}
CodeForces - 1006C Three Parts of the Array
思路
定义一个sum记录前缀和,然后利用系统STL进行二分答案,最后用ans记录答案;
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
ll n,c;
ll x;
ll sum[N];
ll ans=0;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>x;
sum[i]=sum[i-1]+x;
}
for(int i=1;i<=n;i++){
int l=i+1;
int r=n;
int pos=lower_bound(sum+1,sum+1+n,sum[r]-sum[l-1])-sum;
if(pos>=i&&sum[pos]==sum[r]-sum[l-1]){
ans=sum[i];
}
}
cout<<ans<<endl;
return 0;
}
CodeForces - 1041D Glider
思路
定义sum和ans分别记录上升气流和非上升气流的区域,最后系统STL进行二分答案得到结果;
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
int n;
int h;
int s[N];
int e[N];
int sum[N];
int ans[N];
int main(){
cin>>n>>h;
for(int i=1;i<=n;i++){
cin>>s[i]>>e[i];
if(i>1){
sum[i]=sum[i-1]+(s[i]-e[i-1]);
}
ans[i]=ans[i-1]+(e[i]-s[i]);
}
int res=0;
for(int i=1;i<=n;i++){
int pos=lower_bound(sum+i+1,sum+n+1,h+sum[i])-sum;
--pos;
res=max(res,(ans[pos]-ans[i-1]));
}
cout<<res+h;
return 0;
}
CodeForces - 1915E Romantic Glasses
思路
定义一个sumj,sumo分别记录奇数和偶数下标,然后进行枚举如果找到答案,直接结束循环
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
ll t,n;
ll sumj[N];
ll sumo[N];
ll a[N];
map<ll,int> cnt;
int main(){
cin>>t;
while(t--){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
if(i%2==1){
sumj[i]=sumj[i-1]+a[i];
sumo[i]=sumo[i-1];
}else{
sumj[i]=sumj[i-1];
sumo[i]=sumo[i-1]+a[i];
}
}
bool f=1;
for(int r=0;r<=n;r++){
cnt[sumj[r]-sumo[r]]++;
if(cnt[sumj[r]-sumo[r]]>1){
cout<<"YES"<<endl;
f=0;
break;
}
}
if(f==1){
cout<<"NO"<<endl;
}
cnt.clear();
}
return 0;
}