7-28课程解析+总结
题目解析+总结
T637969
有一个长度为的数组,请你执行次操作:
- 移除的第一个元素,并在的尾部追加一个。
输出操作后中的所有元素。
分析
这里没啥好讲的,但是需要注意一个易错点:。所以,我们输出时需要取和的最小值。
for(int i=1;i<=min(n,k);++i) cout<<0<<' ';代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;++i){
int a; cin>>a;
if(i > k) cout<<a<<' ';
}
for(int i=1;i<=min(n,k);++i) cout<<0<<' ';
return 0;
}总结
模拟法
T638034
给出一场有向图,现在你对这张图进行了次操作。每次操作给出三个整数、、。其含义如下:
- :连接和
- :取消连接和
- :查询和是否互相连接
对于每个,要是满足,输出Yes,否则输出No
分析
因为数据量达到了惊人的,所以我们要用二维map。
map<int,map<int,bool>>op;每次或,都要将他们进行连接。
if(op1 == 1) op[a][b] = 1;
if(op1 == 2) op[a][b] = 0;然后是的情况,详细见题意。
if(op1 == 3){
if(op[a][b] && op[b][a]) cout<<"Yes\n";
else cout<<"No\n";
}代码
#include<bits/stdc++.h>
using namespace std;
int n,q;
map<int,map<int,bool>>op;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>q;
while(q--){
int op1,a,b;
cin>>op1>>a>>b;
if(op1 == 1) op[a][b] = 1;
if(op1 == 2) op[a][b] = 0;
if(op1 == 3){
if(op[a][b] && op[b][a]) cout<<"Yes\n";
else cout<<"No\n";
}
}
return 0;
}总结
map + 模拟法
T638156
有一个长度为的数组,你要对它执行三种操作,每次操作都会读入一个整数:
- ,读入整数,表示将数组的元素全部替换为;
- ,读入两个整数,表示
- ,读入整数,查询的值。
对于每个,输出结果。
分析
操作一:
记录操作1的最后执行时间:last_t = i;
记录操作1的最后执行的:last_k = k;
操作二:
if(last_t == 0 || t[x] > last_t) a[x] += k;
if(t[x] < last_t) a[x] = k;
t[x] = i;操作三:
if(last_t == 0) cout<<a[x]<<endl;
else if(last_t > t[x]) cout<<last_k<<endl;
else if(last_t < t[x]) cout<<a[x]+last_k<<endl;代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q,a[200005],t[200005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i) cin>>a[i];
cin>>q;
int last_t = 0,last_k = 0,i = 0;
while(q--){
int op;
cin>>op;
i++;
if(op == 1){
int k;
cin>>k;
last_t = i;
last_k = k;
}
else if(op == 2){
int x,k;
cin>>x>>k;
if(last_t == 0 || t[x] > last_t) a[x] += k;
if(t[x] < last_t) a[x] = k;
t[x] = i;
}
else if(op == 3){
int x;
cin>>x;
if(last_t == 0) cout<<a[x]<<endl;
else if(last_t > t[x]) cout<<last_k<<endl;
else if(last_t < t[x]) cout<<a[x]+last_k<<endl;
}
}
return 0;
}总结
数学 + 模拟法
T638430
给你个实数区间。第 个区间是 。求有多少对,中,有多少个和两个区间相交?
分析
如果两个区间不相交,有以下两种情况:
相交对数 = 区间总长度 - 不相交的对数
所以统计出不相交的对数即可。
使用数组存储所有的左端点,然后排序,二分查找第一个的位置,; 使用数组存储所有的右端点,然后排序,二分查找第一个的位置,
int sum = 0;
for(int i=1;i<=n;++i){
int pos1 = upper_bound(b+1,b+1+n,right1[i]) - b;
int pos2 = lower_bound(c+1,c+1+n,left1[i]) - c;
sum += n-pos1+1;
sum += pos2-1;
}
cout<<n*(n-1)/2-sum/2<<endl;代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int left1[500005],right1[500005],b[500005],c[500005],n;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i){
cin>>left1[i]>>right1[i];
b[i] = left1[i],c[i] = right1[i];
}
sort(b+1,b+1+n);
sort(c+1,c+1+n);
int sum = 0;
for(int i=1;i<=n;++i){
int pos1 = upper_bound(b+1,b+1+n,right1[i]) - b;
int pos2 = lower_bound(c+1,c+1+n,left1[i]) - c;
sum += n-pos1+1;
sum += pos2-1;
}
cout<<n*(n-1)/2-sum/2<<endl;
return 0;
}总结
二分查找 + 数学