T735987 计算题
#include<bits/stdc++.h>
using namespace std;
int a,b,c,t;
char s;
int main(){
cin>>t;
while(t--){
a=0;
b=0;
c=0;
cin>>a>>s>>b>>s>>c;
a+=b;
if(a==c){
cout<<"Right!"<<endl;
}
else{
cout<<"Wrong!"<<endl;
}
}
}根据题意模拟即可;
P14358 [CSP-J 2025] 座位
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 2e5 + 10;
int n, m, R, a[N];
bool cmp(int x, int y) {
return x > y;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n * m; i++){
cin >> a[i];
}
R = a[1]; // 记录r的分数,保证不重复
sort(a + 1, a + 1 + n*m, cmp); // 排序
int idx = 0;
for (int j = 1; j <= m; j++){
if(j % 2 == 1) // 如果是奇数那么从上到下
{
for (int i = 1; i <= n; i++){
idx++;
if (a[idx] == R)
{
cout << j << " " << i << endl;
return 0;
}
}
}
else // 如果是偶数那么从下到上
{
for (int i = n; i >= 1; i--){
idx++;
if (a[idx] == R)
{
cout << j << " " << i << endl;
return 0;
}
}
}
}
return 0;
}T735965 愤怒的程序员
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N=2e5+10;
long long n,k;
long long 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+n+1);
for(int i=1;i<=n;i++){
ans[i]=lower_bound(b+1,b+n+1,a[i])-b-1;//第i个人的总学徒数
}
while(k--){
long long x,y;
cin>>x>>y;
if(a[x]>a[y]){//如果y可以当x的学徒且有争吵,就学徒数--
ans[x]--;
}
if(a[x]<a[y]){//如果x可以当y的学徒且有争吵,就学徒数--
ans[y]--;
}
}
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
}
return 0;
}T735969 选队员
⽤双向链表模拟学⽣队列, pre[i] 和 nxt[i] 分别记录位置 i 左右最近的未被选学⽣。 因为每次要选能⼒值最⼤的学⽣,⽤ pos[v] 记录能⼒值 v 对应的位置,从⼤到⼩遍历能⼒ 值 v 。每次找到未被选的最⼤能⼒学⽣后,向左右各扩展 k 个学⽣,标记队伍(⽤ team 变量交替 1 和 2),然后更新链表删除这些学⽣。 重点提示: 初始化 pre[i] = i-1 , nxt[i] = i+1 ,边界 pre[1] = 0 , nxt[n] = n+1 。 找到要删除的区间 [L, R] 后,将 pre[L] 的 nxt 连到 nxt[R] , nxt[R] 的 pre 连到 pre[L] ,完成节点删除。 队伍切换⽤ team = 3 - team ,简洁⾼效。
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N=2e5+10;
int pre[N],nxt[N],a[N],pos[N],ans[N];
int main(){
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
pos[a[i]]=i;
pre[i]=i-1;
nxt[i]=i+1;
}
pre[1]=0;
nxt[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(nxt[r]!=n+1)r=nxt[r],ans[r]=team;
else break;
}
nxt[pre[l]]=nxt[r];
pre[nxt[r]]=pre[l];
team=3-team;
}
for(int i=1;i<=n;i++){
cout<<ans[i];
}
cout<<endl;
return 0;
}T735985 书架
开2倍的数组,int a[400010], pos[200010]; L = 200001, R = 200000; 左边加数字:a[—L] = id, pos[id] = L; 右边加数字:a[++R] = id, pos[id] = R; 查询id: cout << min(pos[id]-L, R-pos[id]); 开2倍的数组,int a[400010], pos[200010]; L = 200001, R = 200000; 左边加数字:a[—L] = id, pos[id] = L; 右边加数字:a[++R] = id, pos[id] = R; 查询id: cout << min(pos[id]-L, R-pos[id]); 优化: 去掉a数组
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 2e5 + 10;
int pos[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q;
cin >> q;
int L = 0, R = 0;
bool first = true;
while (q--) {
char op;
int id;
cin >> op >> id;
if (op == 'L') {
if (first) {
pos[id] = 0;
L = 0;
R = 0;
first = false;
} else {
pos[id] = L - 1;
L = pos[id];
}
} else if (op == 'R') {
if (first) {
pos[id] = 0;
L = 0;
R = 0;
first = false;
} else {
pos[id] = R + 1;
R = pos[id];
}
} else if (op == '?') {
cout << min(pos[id] - L, R - pos[id]) << endl;
}
}
return 0;
}