牛客OI周赛7-普及组
牛客OI周赛7-普及组
比赛链接
A救救喵咪
这题非常简单,纯模拟就可以过,不用解释
代码如下
#include<bits/stdc++.h>
using namespace std;
struct gg
{
int x,y;
}a[10005];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i].x>>a[i].y;
for(int i=1;i<=n;i++)
{
int ans=0;
for(int j=1;j<=n;j++)
if(i!=j&&a[j].x>a[i].x&&a[j].y>a[i].y)
ans++;
cout<<ans<<endl;
}
}
B救救兔子
很明显是二分,但我
为装逼用了 set。set 自带 去重和排序,还有lower_bound(x)函数可以返回大于或等于 x的第一个数
lower_bound(x)返回的就是大于或等于x的第一个数,有因为排好了序,所以lower_bound(x)与它前面一个数是x在数列中两端的数
- 注意:不能超出set边界——血的教训记心中
看代码
#include<bits/stdc++.h>
using namespace std;
set <int> a;
int main()
{
int n,m;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
a.insert(x);
}
cin>>m;
for(int i=1;i<=m;i++)
{
int x;
cin>>x;
set<int>::iterator it1=a.lower_bound(x);//获取大于或等于x的第一个数
set<int>::iterator it2=it1;
if(it1==a.end())//边界维护
{
cout<<(*(--a.end()))<<endl; //--一定要加,否着就只有80分
continue;
}
if(it1!=a.begin())//边界维护
it1--;//获取比x小的第一个数
if(x-(*it1)<=(*it2)-x)
cout<<(*it1)<<endl;
else
cout<<(*it2)<<endl;
}
}
C救救企鹅
这个题目本蒟蒻粗略一看非常简单,细细地看还是很简单。
使用string自带的函数就可以AC了
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s,a,b;
cin>>s>>a>>b;
while(1==1)
{
int tot=s.find(a);
if(tot==-1) break;
s.erase(tot,a.size());
s.insert(tot,b);
}
cout<<s;
}
D数糖纸
这题目看起来仍然简单。但最开始我还以为要把所有区间全部枚举一遍。然后我打出暴力的时候想出来一种优化方案,有价值的是两个相同的颜色的糖纸之间的区间。然后在我把这个优化方案打出来的时候,突然意识到我只需要做一个类似单调队列的程序就可以A,于是,这题就死在了我的智慧
(乱搞)上
看
#include<bits/stdc++.h>
using namespace std;
bool ma[1000000005]; //ma[x]表示队列中是否出现过x
int main()
{
int n,ans=0,len=0;
queue <int> q;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
if(ma[x]==0)//若队列中没有这种颜色,就能放入颜色
{
q.push(x);
len++;
ma[x]=1;
}
else //必须要加,因为上一行有 ma[x]=1;
if(ma[x]==1)//把在x处截断彩纸 引进新色,就要剔除旧色
{
q.push(x);
while(q.front()!=x)
{
ma[q.front()]=0;
len--;
q.pop();
}
q.pop();
}
ans=max(maxn,len);//储存最长的糖纸
}
cout<<ans;
}