8.17-407-模拟考
分数
| 总分:364 | 转变 | 真实社交 | 落落的去维护数组 | 交叉区间 | 课前测:敲鼓 |
|---|---|---|---|---|---|
| 分数 | 100 | 50 | 74 | 40 | 100 |
T1 转变
题意分析
题意简化
给你一串数,让你把前 个数去掉,然后在后面补上个0。
这一题第一眼看成了双端队列,但仔细一想,我们只要从第 个数输出,后面补上 个0即可。
注意特判 大于 的情况。
代码
代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=105;
int n,k;
int a[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
if(k>n){
for(int i=1;i<=n;i++){
cout<<0<<' ';
}
return 0;
}
for(int i=k+1;i<=n;i++){
cout<<a[i]<<' ';
}
for(int i=1;i<=k;i++){
cout<<0<<' ';
}
return 0;
}T2 真实社交
题意分析
死因
死因
不是, 是给数组完的吗?
错误代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e4+5;
int n,q;
bool G[N][N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
while(q--){
int op,x,y;
cin>>op>>x>>y;
if(op==1){
G[x][y]=1;
}else if(op==2){
G[x][y]=0;
}else{
if(G[x][y]==1&&G[y][x]==1){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
}
return 0;
}题意分析
题意简化
有 次操作,操作1为在 与 之间建一条边,操作2为在 与 之间建拆一条边,操作3为问 与 之间有没有两条边。
第一眼看成了图,结果看到数据直接劝退,其实我们可以用 和 做对。只要把 和 的关注关系映射成零或一即可。
代码
代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e4+5;
int n,q;
map<pair<int,int>,int> mp;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
while(q--){
int op,x,y;
cin>>op>>x>>y;
if(op==1){
mp[{x,y}]=1;
}else if(op==2){
if(mp.count({x,y})){
mp.erase({x,y});
}
}else{
if(mp.count({x,y})&&mp.count({y,x})){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
}
return 0;
}T3 落落的去维护数组
死因
死因
写暴力,结果大数据炸飞了。
错误代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=2e5+5;
int n,q;
int a[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
cin>>q;
while(q--){
int op;
cin>>op;
if(op==1){
int k;
cin>>k;
for(int i=1;i<=n;i++){
a[i]=k;
}
}else if(op==2){
int i,k;
cin>>i>>k;
a[i]+=k;
}else{
int i;
cin>>i;
cout<<a[i]<<endl;
}
}
return 0;
}题意分析
题意简化
我们可以定义一个 作为偏移量, 为上一次整体清空的时间,数组 为上一次单体清空的时间。
操作为1时, 为 , 为 。操作为2是判断 是否大于 如果是加上。否则,先清空在加。操作3同理。
代码
代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=2e5+5;
int n,q;
int a[N],t[N];
int lasttime,lastval;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
cin>>q;
for(int i=1;i<=q;i++){
int op,x,k;
cin>>op;
if(op==1){
cin>>k;
lasttime=i;
lastval=k;
}else if(op==2){
cin>>x>>k;
if(lasttime==0){
a[x]+=k;
}else if(t[x]>lasttime){
a[x]+=k;
}else{
a[x]=0;
a[x]+=k;
}
t[x]=i;
}else{
cin>>x;
if(lasttime==0){
cout<<a[x]<<endl;
}else if(t[x]>lasttime){
cout<<lastval+a[x]<<endl;
}else{
cout<<lastval<<endl;
}
}
}
return 0;
}T4 交叉区间
死因
死因
没有万全。
错误代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=5e5+5;
int n;
struct node{
int l,r;
}q[N];
bool cmp(node x,node y){
return x.l<y.l;
}
int ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>q[i].l>>q[i].r;
}
sort(q+1,q+n+1,cmp);
bool f=0;
for(int i=2;i<=n;i++){
if(q[i].l<=q[i-1].r){
ans++;
}
}
if(q[n].l<=q[1].r){
ans++;
}
cout<<ans;
return 0;
}题意分析
题意简化
既然直接求不好求,那我们可以先把总对数算出来,再把不相交的数量算出来,一减就出来了。
第一步是排序。先算前面不相交的,就是小于 的,我们只要用 的坐标减一即可,然后算后面不相交的,就是大 的,算出第一个大于 的即可。
OK,其实就是二分!
代码
代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=5e5+5;
int n;
struct node{
int l,r;
}q[N];
int a[N],b[N];
int ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>q[i].l>>q[i].r;
}
for(int i=1;i<=n;i++){
a[i]=q[i].l;
}
for(int i=1;i<=n;i++){
b[i]=q[i].r;
}
sort(a+1,a+n+1);
sort(b+1,b+n+1);
for(int i=1;i<=n;i++){
int L=q[i].l,R=q[i].r;
int x=upper_bound(a+1,a+n+1,R)-a;
// cout<<x<<' ';
int num1=n-x+1;
x=lower_bound(b+1,b+n+1,L)-b;
// cout<<x<<endl;
int num2=x-1;
ans+=num1+num2;
}
ans/=2;
int x=n*(n-1)/2;
cout<<x-ans;
return 0;
}