T735987 计算题
题意
有一些加法题,检查这些题目算没算对
思路
以加号和等号为分界线,存储三个数字,转化成加法运算计算是否正确
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
void op(string s){
int a = 0;
int b = 0;
int c = 0;
bool f = false;
bool f2 = false;
for(int i = 0;i < s.size();i++){
if(s[i] >= '0' and s[i] <= '9'){
if(!f){
a = a * 10 + (s[i] - '0');
}else if(!f2){
b = b * 10 + (s[i] - '0');
}else{
c = c * 10 + (s[i] - '0');
}
}else if(s[i] == '+'){
f = true;
}else if(s[i] == '='){
f2 = true;
}
}
if(a + b == c){
cout << "Right!"<< '\n';
}else{
cout << "Wrong!"<< '\n';
}
}
int n;
string s;
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
while(n--){
cin >> s;
op(s);
}
return 0;
}
/*
5
1+1=2
1+2=2
15+66=81
5+37=40
99+99=99
*/T736073 座位
题意
有一帮人要坐座位,每人有一个分数,由高到低蛇形分配座位,分数越高的人越先坐到座位,请问第一个人做到第几列第几行
思路
先排序一下每个人的分数,这里要用结构体数组标记所有的分数位置,然后模拟座位过程即可,发现现在排的分数等于时,就输出当前列和行
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
struct P{
int v;
int id;
}a[105];
bool cmp(P a, P b){
return a.v > b.v;
}
int c = 1, r; // c列 r行
signed 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].v;
a[i].id = i;
}
sort(a + 1, a + 1 + n * m, cmp);
for(int i = 1;i <= n * m;i++){
if(c % 2 == 1){
if(r != n){
r++;
}else{
c++;
}
}else{
if(r != 1){
r--;
}else{
c++;
}
}
if(a[i].id == 1){
cout << c<< ' '<< r;
return 0;
}
}
return 0;
}
/*
2 2
99 100 97 98
*/
/*
2 2
98 99 100 97
*/
/*
3 3
94 95 96 97 98 99 100 93 92
*/T735965 愤怒的程序员
题意
有一帮人要选导师,每个人有一个能力值,如果的能力值大于的能力值,且他们没有争吵,可以当的导师,问每个人可以给多少个人当导师
思路
我们可以用二分查找找到在不计算争吵的前提下每个人比他能力值小于等于的人有几个,再检查每个人的关系,如果有争吵,就会对能力值最大的人有影响,如果能力值相同对两人都有影响
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2 * 1e5 + 5;
int n, k;
int a[N], b[N];
vector <int> v[N];
int ans[N];
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
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++){
int pos = lower_bound(b + 1, b + 1 + n, a[i]) - b;
ans[i] = pos - 1;
}
while(k--){
int r, q;
cin >> r>> q;
if(a[r] > a[q]){
ans[r]--;
}else if(a[r] < a[q]){
ans[q]--;
}else{
ans[r]--;
ans[q]--;
}
}
for(int i = 1;i <= n;i++){
cout << ans[i]<< ' ';
}
return 0;
}T735969 选队员
题意
有两个教练要挑人组队,有人能挑选,挑选方法如下
- 教练一先挑,教练二后挑
- 每个教练选择现在队伍中能力值最强的那个
- 把能力值最强的左边人和右边人也挑走 请问每个人是加入了一对还是二队
思路
用两个数组分别记录每个位置左右最近的学生,一个桶记录每个能力值的学生位置。把桶数组从大到小遍历,找到学生的位置后像左右各遍历个学生,就可以标记上他们加入的队伍并将它们移除
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
const int N = 2 * 1e5 + 5;
int a[N];
int pos[N];
int nxt[N], pre[N];
int ans[N];
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> k;
//预处理双向链表和pos
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 > 0;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;
}
}
//删除区间
int l2 = pre[l];
int r2 = nxt[r];
nxt[l2] = r2;
pre[r2] = l2;
team = 3 - team;//更新队伍
}
for(int i = 1;i <= n;i++){
cout << ans[i];
}
return 0;
}T735985 书架
题意
有一个书架,可以把书放到最左边或最右边,会询问把左边或右边拿走多少本书,才能让编号为的书到最左边或者最右边
思路
用一个数组来记录每本书的位置,计算让编号为的书到最左边或者最右边时直接计算就好了
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int pos[N];
int n;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
int l = 1, r = 0;
while(n--){
char op;
cin >> op;
if(op == 'L'){
int id;
cin >> id;
pos[id] = --l;
}else if(op == 'R'){
int id;
cin >> id;
pos[id] = ++r;
}else if(op == '?'){
int id;
cin >> id;
cout << min(pos[id] - l, r - pos[id])<< '\n';
}
}
return 0;
}