总结链 | 他人查看用总结链
比赛
初始分数我忘了
总结
T1
我们知道,cin是分类型输入的,比如:
int a;
string s
cin >> a >> s;
---
输入:666synb这样a==666;s==synb
所以我们可以:
int a,b,d;//代表两加数和和
char c;//连接中间的+和=然后cin>>a>>c>>b>>c>>d;,因为c是用于连接,无实际作用,所以可以重复利用
然后就是简单的判断:
如果(a+b==c) 输出 << "";
否则 输出<< "";
换行代码:
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pii pair<int,int>
#define piii pair<int,pii>
using namespace std;
constexpr int N=1e5+5,M=5*N,K=5000;
void solve(){
int n,m,k;
char c;
cin >> n >> c >> m >> c >> k;//cin能分类型输入
if(n+m==k) cout << "Right!";//对(蒸蚌)
else cout << "Wrong!";//错
cout << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t;
cin >> t;//t组数据
while(t--){
solve();//把代码写成solve更简洁一些
}
return 0;
}T2
按照题意模拟即可,注意行数与列数不要搞错
行数:
if(c%2==1){
r = pos;
}else{
r = n-pos+1;
}列数:
ll c = (x-1)/n+1;最终代码:
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N = 1e5+5,M = 5*N,K = 5000;
struct node{
ll num,id;//结构体,待会儿要记两个参数,我不会pii排序所以不用pii
};
bool cmp(node x,node y){
return x.num>y.num;//排序
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
ll n,m;
cin >> n >> m;
node a[N];
for(ll i = 1;i<=n*m;i++){//输入
cin >> a[i].num;
a[i].id = i;
}
sort(a+1,a+1+n*m,cmp);//排序
ll x = 0;//小R的位置
for(ll i = 1;i<=n*m;i++){
if(a[i].id==1){
x = i;
break;
}
}
ll c = (x-1)/n+1;//行与列
ll pos = x-(c-1)*n;
ll r;
if(c%2==1){
r = pos;
}else{
r = n-pos+1;
}
cout << c << ' ' << r << endl;//输出
return 0;
}T3
先录入,建一个数组备份原录入数组
再将备份数组排序,建数组ans,定义:
使用二分是因为排序后二分查找的左部分都比小,便是我们需要的答案
重复k次,每次输入两个数,让
最后输出即可
代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N = 2e5 + 10;
ll n,k;
ll a[N],b[N],ans[N];//数组
int main(){
cin >> n >> k;
for(int i = 1;i<=n;i++){
cin >> a[i];
b[i] = a[i];//备份
}
sort(b+1,b+1+n);
for(int i = 1;i<=n;i++){
ans[i] = lower_bound(b+1,b+1+n,a[i])-b-1;//排序
}
while(k--){
ll x,y;
cin >> x >> y;
if(a[x]>a[y]){//减去一个答案
ans[x]--;
}
if(a[x]<a[y]){
ans[y]--;
}
}
for(int i = 1;i<=n;i++){
cout << ans[i] << " ";//逐个输出
}
return 0;
}T4
首先录入实力与下标
然后按题意模拟
代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N = 2e5+10;
int pre[N],nt[N],a[N],pos[N],ans[N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,k;
cin >> n >> k;//录入
for (int i = 1;i<=n;i++) {
cin >> a[i];
pos[a[i]] = i; //下标标记
pre[i] = i-1;
nt[i] = i+1;
}
pre[1] = 0;//补充
nt[n] = n+1;
int team = 1;//团队
for (int v = n;v>=1;v--) {//枚举
int cur = pos[v];//模拟
if (ans[cur]!=0) continue;
ans[cur] = team;//选团
int l = cur,r = cur;
for (int i = 0;i<k;i++) {
if (pre[l]!=0) l = pre[l],ans[l] = team;
else break;
}
for (int i = 0;i<k;i++) {
if (nt[r]!=n+1) r=nt[r],ans[r]=team;
else break;
}
//更新
int lll = pre[l];//ll用不了就用lll
int rr = nt[r];
nt[lll] = rr;//删除l到r
pre[rr] = lll;//删除l到r
team = 3-team;
}
for (int i = 1;i<=n;i++) {//逐个输出
cout << ans[i];
}
return 0;
}T5
按题意模拟即可
所有公式都可以在题目中推到或转化
代码:
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N = 2e5 + 10;
int pos[N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int q;//输入
cin >> q;
int l = 0, r = 0;
int f = 1;
for(int i = 1;i<=q;i++){
char op;//操作
int id;
cin >> op >> id;
if(op=='L'){//是L
if(f==1){//模拟
pos[id] = 0;
l = 0;
r = 0;
f = 0;
}else{
pos[id] = l-1;
l = pos[id];
}
} else if(op=='R'){//是R
if(f==1){//模拟
pos[id] = 0;
l = 0;
r = 0;
f = 0;
}else{
pos[id] = r+1;
r = pos[id];
}
}else if(op=='?'){//啥都不是
cout << min(pos[id]-l,r-pos[id]) << endl;//直接输出
}
}
return 0;
}