版本比较
CSP-J模拟祭
正文
增加 377 行删除 10 行
839 行未改动全部展开840841842843844844845846847848355 行未改动全部展开120412051206120712081209120812091210121112131214121212131214121512181219121612171218121912207 行未改动122812291230123112351232123312341235123622 行未改动1259126012611262126612671263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635839 行未改动全部展开**60pts**
题意:给定一个01串,求其中一个最长连续子串,要求这个子串中 $0$ 和 $1$ 的个数相等。
本来以为是二分,实则想多了。题目做法非常像 [k倍区间](https://www.luogu.com.cn/problem/P8649)本来以为是二分,实则想多了。题目做法非常像 [k倍区间](https://www.luogu.com.cn/problem/P8649)。然而,这题竟然是[P1114](https://www.luogu.com.cn/problem/P1114)原题!
代码:```cpp line-numbers#include<bits/stdc++.h>355 行未改动全部展开
## 10.17**330pts 第一**
[比赛](https://www.luogu.com.cn/contest/365095)
[比赛](https://www.luogu.com.cn/contest/365095) 题目链接在比赛 T1
### T1 鸡块**100pts AC**
**100pts AC** 简单的数学题,秒了。
### T2 套盒子**100pts AC**
**100pts AC** 桶排秒了。
### T3 快速求和**30pts TLE**7 行未改动
---暴力拿了30pts
正解:不考虑 $d(x,y) = 0$ 的情况下,每个 $a_i$ 会对总和贡献 $a_i \times (i-1) - a_i \times (n-i) = a_i \times (2i-n-1)$。正解:不考虑 $d(x,y) = 0$ 的情况下,每个 $a_i$ 会对总和贡献 $a_i \times (i-1) - a_i \times (n-i) = a_i \times (2i-n-1)$。
同时,我们用数组 $b$ 桶排序记录下每个 $a_i$ 在此前出现的次数,算上 $d = 0$ 的情况每个 $a_i$ 还造成了 $b_{a_i+1} - b_{a_i-1}$的贡献。
代码也是很简单:22 行未改动
### T4 字符串变换**100pts AC**
貌似是洛谷原题。
貌似是洛谷原题。 错解AC,我也是很无语,这题算是小模拟了。此处就不贴代码了~~反正是错解~~。
## 10.22**215pts 第四** [比赛](http://120.55.40.84/contest/6ad08b309b6f013640e65f56)
### T1 循环铭文**100pts AC** 小模拟略。### T2 能量补给**100pts AC** 简单的前缀和+二分,略。### T3 共振频率**0pts WA+TLE**
[T3题面](https://www.luogu.com.cn/problem/U745260)
我的思路: 找出所有数的因数,出现了两次及以上的因数视为“优秀因数”,这个情况下ta就可以被表示为两个数的**公因数**(错因:误认为这个数就是最大公因数),若一个优秀因数没有出现在 $a$ 中,则ta就为一个可以加入的合格因数。 但是我很不理解为什么样例全过交上去却全WA,样例这么水数据这么强吗?
正解思路:
正解代码(应该还是很好理解的):```cpp line-numbers#include<bits/stdc++.h>using namespace std;bool bk[1100000];int a[11000000];int main(){ int n; cin>>n; int ma = 0; for(int i=1;i<=n;i++){ cin>>a[i]; bk[a[i]] = 1; ma = max(ma,a[i]); } int ans = 0; for(int i=1;i<=ma;i++){ if(bk[i]) continue; int g = 0; for(int j=2*i;j<=ma;j+=i){ if(bk[j]) g = __gcd(g,j); } ans += (g == i); } cout<<ans; return 0;}```
### T4 载荷排行**15pts WA**
[T4题面](https://www.luogu.com.cn/problem/U745262)
这题拿到手根本就不会写,于是就瞎写了一个,还拿了15pts。
正解:
代码也呼之欲出了:```cpp line-numbers#include<bits/stdc++.h>using namespace std;#define int long longstruct bai{ int now; int maxn; }a[310000];bool cmp(bai a,bai b){ return a.now < b.now;}priority_queue<int,vector<int>,greater<int> > q;//优先队列
signed main(){ int n; cin>>n; bai me; cin>>me.now>>me.maxn; for(int i=1;i<=n-1;i++){ cin>>a[i].now>>a[i].maxn; } sort(a+1,a+n,cmp); int i = n-1; int ans = n; while(true){ while(a[i].now > me.now){ q.push(a[i].maxn - a[i].now + 1); i--; } ans = min(ans,(int)q.size()+1); if(!q.empty() && q.top() <= me.now){ me.now -= q.top(); q.pop(); } else break; } cout<<ans; return 0;}
```
## 10.24**280pts 全致远组第一**
[比赛](http://www.accoders.com/contest.php?cid=5362)
题解在任意题中[Solution],题面在T1的附件Zip。
### T1 星灯工坊**100pts AC**
小学二年级就学过的数学题。唯一需要注意的是特判除以0。略。
### T2 档案标签**100pts AC**
小学三年级就学过的贪心,非常简单,秒了。
### T3 静默片段**60pts RE**
类似 [P8649 k倍区间](https://www.luogu.com.cn/problem/P8649),题意:给定一个序列 $a$ ,求有多少个区间的区间异或和为 $0$。形式化地,求有多少组不同的 $(l,r) (l \le r)$ 使得 $a_l \oplus a_{l+1} \oplus ... \oplus a_r = 0$。
思路与 k倍区间 类似,利用前缀和以及桶 $O(n)$ 求出区间个数。
需要注意:桶的大小应该开到 $2^{17}$,否则会 RE,还要关掉`cin` `cout`的缓存,否则会TLE。
代码:```cpp line-numbers#include<bits/stdc++.h>using namespace std;int a[10001000];long long pre[10001000];int bk[210000];
signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; long long z = 0; for(int i=1;i<=n;i++){ cin>>a[i]; pre[i] = pre[i-1] ^ a[i]; z += (bk[pre[i]]); if(pre[i] == 0) z++; bk[pre[i]]++; } if(z == 0) cout<<"liar"; else cout<<z; return 0;}```
### T4 信号走廊**20pts TLE**
题意:
暴力无疑是会超时的。
正解:二分枚举答案,再用 01bfs 搜索。:::info[什么是01bfs?]01bfs是基于bfs的一种优化,适用于数组边权仅为 $0$ 或 $1$ 的情况。它的主要作用是求从那个某一点到某一点的最短路。
对于bfs,01bfs只需要将原来的队列改为双端队列`deque`即可。实现方面来说,需要将bfs版子中的以下几行进行修改 : `queue <int> q` $\to$ `deque <int> q` `q.push(x)` $\to$ `q.push_front(x)` `q.pop()` $\to$ `q.pop_front()`
具体来说可以参考以下代码,修改处已用注释标出。bfs:```cppqueue <int> qx; queue <int> qy;int bk[400][400] = {0};qx.push(1); qy.push(1);//!bk[1][1] = p[1][1];while(!qx.empty()){ int x = qx.front(),y = qy.front(); qx.pop(); qy.pop();//! for(int i=0;i<4;i++){ int xk = x+dx[i],yk = y+dy[i]; if(xk > 0 && yk > 0 && xk <= n && yk <= m){ if(bk[xk][yk] > bk[x][y]+p[xk][yk]){ bk[xk][yk] = bk[x][y] + p[xk][yk]; qx.push(x); qy.push(y);//! } } }}return bk[n][m] <= k;```01bfs:```cppdeque <int> qx; deque <int> qy;int bk[400][400] = {0};qx.push_front(1); qy.push_front(1);bk[1][1] = p[1][1];while(!qx.empty()){ int x = qx.front(),y = qy.front(); qx.pop_front(); qy.pop_front();//! for(int i=0;i<4;i++){ int xk = x+dx[i],yk = y+dy[i]; if(xk > 0 && yk > 0 && xk <= n && yk <= m){ if(bk[xk][yk] > bk[x][y]+p[xk][yk]){ bk[xk][yk] = bk[x][y] + p[xk][yk]; if(p[x][y]) qx.push_back(xk),qy.push_back(yk);//! else qx.push_front(xk),qy.push_front(yk);//! } } }}return bk[n][m] <= k;```:::
代码:```cpp line-numbers#include<bits/stdc++.h>using namespace std;int n,m,k;int a[400][400];int ans = 0;int dx[4] = {0,1,0,-1};int dy[4] = {1,0,-1,0};bool check(int as){ bool p[400][400]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ p[i][j] = (a[i][j] < as); } } queue <int> qx; queue <int> qy; int bk[400][400] = {0}; qx.push(1); qy.push(1); bk[1][1] = p[1][1]; while(!qx.empty()){ int x = qx.front(),y = qy.front(); qx.pop(); qy.pop(); for(int i=0;i<4;i++){ int xk = x+dx[i],yk = y+dy[i]; if(xk > 0 && yk > 0 && xk <= n && yk <= m){ if(bk[xk][yk] > bk[x][y]+p[xk][yk]){ bk[xk][yk] = bk[x][y] + p[xk][yk]; qx.push(x); qy.push(y); } } } } return bk[n][m] <= k;}
int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m>>k; if(k >= n+m-1){ cout<<1000000000; return 0; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>a[i][j]; } } int l = 1,r = 1e9; while(l <= r){ int mid = (l+r) >> 1; if(check(mid)) l = mid + 1; else r = mid - 1; } cout<<r; return 0;}
```
## 10.29**260pts 第一**
### T1 密钥底片**100pts AC**
简单模拟题。
### T2 环形校验值**40pts RE**
算法是对的,不过破环为列后需要把**数组开两倍**,我忘了。。。
### T3 巡检路线**100pts AC**
全场唯一AC,思路类似于[P1535](https://luogu.com.cn/problem/P1535)。
### T4 信号折线校准**20pts WA**
题意:给定一个序列 $a$,进行若干次操作(将 $a$ 中的任意一个元素改为任意数字)后,使得对于任意满足 $2 \le i \le n-1$ 的任意一个 $i$,均有 $a_{i-1} < a_i > a_{i+1}$ 或 $a_{i-1} > a_i < a_{i+1}$,求最少更改次数。
思路:分两种情况讨论:对于任意满足 $2 \le i \le n-1$ 的任意一个 $i$,均有: 情况1. $a_{i-1} < a_i > a_{i+1}$ 情况2. $a_{i-1} > a_i < a_{i+1}$ 接下来依次遍历 $i = 1,2,3,...,n-1$,若不满足应当的 $a_i$ 与 $a_{i+1}$ 的关系,就把 $a_{i+1}$ 改为 $2 \times 10^9$ 或 $-2 \times 10^9$ 即可。
代码:```cpp line-numbers#include<bits/stdc++.h>using namespace std;#define int long longint a[110000];const int I_ma = 2147483647;
signed main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } int ans = 0; int z = 0; int ls[110000] = {0}; for(int i=1;i<=n;i++) ls[i] = a[i]; ls[n+1] = I_ma; for(int i=1;i<n;i++){ if(i%2 == 1){ if(ls[i] <= ls[i+1]){ ls[i+1] = -I_ma; z++; } } else{ if(ls[i] >= ls[i+1]){ ls[i+1] = I_ma; z++; } } } ans = z; z = 0; for(int i=1;i<=n;i++) ls[i] = a[i]; ls[n+1] = I_ma; for(int i=1;i<n;i++){ if(i%2 == 0){ if(ls[i] <= ls[i+1]){ ls[i+1] = -I_ma; z++; } } else{ if(ls[i] >= ls[i+1]){ ls[i+1] = I_ma; z++; } } } ans = min(ans,z); cout<<ans<<'\n'; return 0;}//copy by baibai__```
## 结语终于,获得了一个圆满的结果:
1=没跑了,所有分数均在意料之内,真是一个圆满的CSP。
唯一的遗憾是T3想到正解却犹豫了没写出来。