20240827普及组模拟赛
20240827普及组模拟赛
复盘
T1
签到题,以后要多练一下签到题和暴力水题的速度.
学习@2huk
%%%
预计分数:
T2
赛时用sortlower_bound骗分
预计分数:
T3
一开始手写chk()炸了,后来想拿致敬不可以,总司令
后来磕了一会用sscanf秒了
预计分数:
T4
看到题想了一会决定打暴力.
预计分数:
小结
预计总分数:
实际总分数:
花费时间:
题解
P7909 [CSP-J 2021] 分糖果
思路
注意到,答案
\lfloor\frac{l}{n}\rfloor\ne\lfloor\frac{r}{n}\rfloor$ ,即$\exists x \in[l,r]$满足$ \ x\text{ mod } n= n-1$,答案为$n-1 \lfloor\frac{l}{n}\rfloor=\lfloor\frac{r}{n}\rfloor$,即$\exists x \in[l,r]$满足$r\text{ mod }n\ge x\text{ mod }n\ge l\text{ mod }n$,答案为$r\text{ mod }n
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long LL;
ll n, l, r;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> l >> r;
if(l / n != r / n) {
cout << n - 1 << endl;
} else {
cout << r % n << endl;
}
return 0;
}
P7910 [CSP-J 2021] 插入排序
思路
每次直接修改,查询看有几个排在当前元素前面(注:题面给的代码是稳定排序,所以在当前元素前的要加等号).
€€£数据太水了
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long LL;
constexpr ll N = 8000 + 10;
int n, Q;
int a[N];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> Q;
for(int i = 1; i <= n; ++i) {
cin>>a[i];
}
for(int q = 1; q <= Q; ++q) {
int id, x, v;
cin >> id >> x;
if(id == 1) {
cin >> v;
a[x] = v;
}
if(id == 2) {
int res = 1;
for(int i = 1;i < x; ++i) {
res += a[i] <= a[x];
}
for(int i = x + 1;i <= n; ++i) {
res += a[i] < a[x];
}
cout << res << '\n';
}
}
return 0;
}
P7911 [CSP-J 2021] 网络连接
思路
用sscanf判断字符串格式,前导
剩下的一个unordered_map解决.
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long LL;
ll n;
unordered_map<string, ll> mp;
ll chk(string s) {
ll a, b, c, d, e;
if (sscanf(s.c_str(), "%lld.%lld.%lld.%lld:%lld", &a, &b, &c, &d, &e) != 5) {
return 0;
}
string sa = to_string(a), sb = to_string(b), sc = to_string(c), sd = to_string(d), se = to_string(e);;
if (sa.size() + sb.size() + sc.size() + sd.size() + se.size() + 4 != s.size()) {
return 0;
}
if (!(0 <= a && a <= 255 && 0 <= b && b <= 255 && 0 <= c && c <= 255 && 0 <= d && d <= 255 && 0 <= e && e <= 65535)) {
return 0;
}
return 1;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (ll i = 1; i <= n; ++i) {
string s1, s2;
cin >> s1 >> s2;
if (!chk(s2)) {
cout << "ERR" << endl;
continue;
}
if (s1 == "Server") {
if (mp.count(s2)) {
cout << "FAIL" << endl;
continue;
} else {
mp[s2] = i;
cout << "OK" << endl;
continue;
}
}
if (s1 == "Client") {
if (mp.count(s2)) {
cout << mp[s2] << endl;
continue;
} else {
cout << "FAIL" << endl;
continue;
}
}
}
return 0;
}
P7912 [CSP-J 2021] 小熊的果篮
思路
ODT秒了
用set模拟块,删后再合并.
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long LL;
constexpr int N = 2e5 + 10;
int n;
int a[N];
struct node {
mutable int l, r, v;
bool operator < (const node b) const {
return l < b.l;
}
};
int vis[N];
set<node> s;
int main(){
cin >> n;
for (ll i = 1; i <= n; ++i) {
cin >> a[i];
}
for (ll i = 1; i <= n; ++i) {
int l = i;
while (i <= n && a[i] == a[l]) ++i;
--i;
s.insert({l, i, a[i]});
}
while (s.size()) {
for (set<node>::iterator it = s.begin(); it != s.end();) {
while (vis[it->l]) {
++it->l;
}
cout << it->l << ' ';
vis[it->l] = 1;
if (it->l == it->r) {
set<node>::iterator tmp = it;
++it;
s.erase(tmp);
} else {
++it->l;
++it;
}
}
for (set<node>::iterator it = s.begin(); it != s.end();) {
set<node>::iterator l = it;
while (it != s.end() && it->v == l->v){
++it;
}
--it;
int pl = l->l, pr = it->r, pv = l->v;
++ it;
s.erase(l, it);
s.insert({pl, pr, pv});
}
cout << '\n';
}
return 0;
}