课下做题记录 2024.8
Zskioaert1106 · · 个人记录
前情提要:
各位家长、同学,本周训练已经结束,
……
课下作业:每天至少做一道题(题目任选),要求至少14道题,下次上课检查。
关于我光 8 月 4 号一天就做了 14 道题这件事
8.5
-
P5886 Hello, 2020!
-
P6056 [加油武汉] SIR 模型
-
P6194 [EER1] 苏联人
-
P6195 [EER1] 迫害
前两题比较水,就不说什么了。
P5886:AC 记录
P6056:AC 记录
P6194 [EER1] 苏联人
简单的模拟,判断战车和主教的各种情况,存储在布尔数组里。将每个不满足条件的点设为 true,最后取反输出即可。
P6194:AC 记录
P6195 [EER1] 迫害
"In Germany they first came for the Communists,
and I didn't speak up because I wasn't a Communist.
Then they came for the Jews,
and I didn't speak up because I wasn't a Jew.
Then they came for the trade unionists,
and I didn't speak up because I wasn't a trade unionist.
Then they came for the Catholics,
and I didn't speak up because I was a Protestant.
Then they came for me ,
and by that time no one was left to speak up."
EER1 的题目都好会出啊
本题主要考察的是数学思维和快速幂。
观察题目可以发现,最初的
这样看来,第二个自定数应设为
于是我们可以打出如下代码:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
long long n,m;
cin>>n>>m;
cout<<int(pow(2,m)*(n+1)-1)%100000007;
return 0;
}
结果是 50 分。因为
快速幂用分治的思想将二进制下的幂运算边计算边取模,正好适合这种情况。于是:
#include<iostream>
using namespace std;
const int p=1000000007;
int q(int a,int b){
if(b==0)return 1;
long long t=q(a,b/2);
if(b&1)return t*t%p*a%p;
return t*t%p;
}
int main(){
long long n,m;
cin>>n>>m;
cout<<(q(2,m)%p*(n+1)-1)%p;
return 0;
}
这样可以 AC。
P6195:AC 记录
8.6
-
P2256 一中校运会之百米跑
-
P2814 家谱
-
P2330 [SCOI2005] 繁忙的都市
-
P1547 [USACO05MAR] Out of Hay S
-
P1888 三角函数
-
P2367 语文成绩
-
SP2 PRIME1 - Prime Generator
P2256 一中校运会之百米跑
一道套上了字符串的并查集板子题,正好借此复习下并查集和路径压缩:
int find(int x){
if(f[x]!=x)f[x]=find(f[x]);
return f[x];
}
此外还需要将字符串与数组下标对应起来。当然我们可以写哈希或者用 map 映射,但是在本题宽松的时间复杂度内,我们直接存个数组每次遍历就好了。
int findname(string s){
for(int i=1;i<=n;i++){
if(a[i]==s)return i;
}
}
然后的一切就顺理成章了。我们先输入
第一遍:万紫千红 20 分。
我在合并时写的是这样:f[findname(s1)]=findname(s2)。没错,合并的压根不是祖先。
于是我改成了 f[find(findname(s1))]=find(findname(s2)),又交了一遍。
第二遍:万绿丛中一点紫的 80 分。
2 RE 了。一开始我以为是数组开小了,但是事实告诉我并非这么回事。怀着惊疑不定的心情,我点开了讨论区—— 如果你 WA #2,注意体育老师可以问本来就不在的选手。
体育老师真所以我的错误是在于 find 函数遍历完后如果都没有查询到就不会返回,而我们的数组范围是 return 0 查询时的效果是一样的。
这是我的最终代码:
#include<iostream>
using namespace std;
int n,m,k,f[20005];
string a[20005];
int findname(string s){
for(int i=1;i<=n;i++){
if(a[i]==s)return i;
}
return 0;
}
int find(int x){
if(f[x]!=x)f[x]=find(f[x]);
return f[x];
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)f[i]=i;
int i;
for(int i=1;i<=n;i++){
string s;
cin>>s;
a[i]=s;
}
string s1,s2;
while(m--){
cin>>s1>>s2;
f[find(findname(s1))]=find(findname(s2));
}
cin>>k;
while(k--){
cin>>s1>>s2;
if(find(findname(s1))==find(findname(s2)))cout<<"Yes.\n";
else cout<<"No.\n";
}
return 0;
}
第三遍:AC 了。
P2256:AC 记录
P2814 家谱
给出充足的父子关系,要求找到某个人的最早的祖先。这题同样是一道套上字符串的并查集板子题。不过我不想再套一遍字符串了,所以学习了一下 map。
map
STL 中的 map 是一个存储键值对并自动排序的容器,跟 Python 里的字典很像。map 的定义方法是:map<type,type>p,其中第一个 type 是键的类型,第二个 type 是值的类型。
一些关于 map 的函数:
-
赋值:用等号可以将一个 map 的值赋值给另一个 map。
-
交换:用 swap 函数可以将两个 map 中的所有元素交换。
-
容量:用
map.size()可以读取 map 存储的键值对数量。 -
map.empty()判断 map 是否为空。 -
map[a]=b:将 map 中键为 a 的值设为 b。 -
清空:
clear()指令可以清除一个 map 中所有的元素。 -
erase()可以删除 map 中指定的元素。erase()的括号中如果只有一个元素,那么它可以是一个键或一个值,map 中相应的键值对会被删除;如果是两个值,那删除的就是一个区间内的所有元素。
有了 map,代码的编写就很容易了。
#include<bits/stdc++.h>
using namespace std;
map<string,string>f;
string father,son;
string find(string x){
if(f[x]!=x)f[x]=find(f[x]);
return f[x];
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
char c;
while(cin>>c){
if(c=='#'){
cin>>father;
if(f[father]=="")f[father]=father;
}
else if(c=='+'){
cin>>son;
f[son]=father;
}
else if(c=='?'){
cin>>son;
cout<<son<<' '<<find(son)<<'\n';
}
else return 0;
}
}
P2814:AC 记录
P2330 [SCOI2005] 繁忙的都市
05 年的四川省选居然是一道最小生成树板子题! 正好复习一下 Kruskal 算法。
Kruskal 算法将一个连通块当做一个集合。首先将所有的边按从小到大顺序排序,并认为每一个点都是孤立的,分属于
n 个独立的集合。然后按顺序枚举每一条边。如果这条边连接着两个不同的集合,那么就把这条边加入最小生成树,这两个不同的集合就合并成了一个集合;如果这条边连接的两个点属于同一集合,就跳过。直到选取了n-1 条边为止。——《最小生成树详解》
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,f[301],s,mx;
struct road{
int a,b,l;
}a[8000];
int find(int x){
if(f[x]!=x)f[x]=find(f[x]);
return f[x];
}
bool cmp(road a,road b){
return a.l<b.l;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)f[i]=i;
for(int i=0;i<m;i++)cin>>a[i].a>>a[i].b>>a[i].l;
sort(a,a+m,cmp);
for(int i=0;i<m;i++){
if(find(a[i].a)!=find(a[i].b)){
mx=a[i].l;
s++;
f[find(a[i].a)]=find(a[i].b);
if(s+1==n)break;
}
}
cout<<s<<' '<<mx;
return 0;
}
P2330:AC 记录
P1547 的代码跟 P2330 几乎一样,不过只用求最小生成树中的最长边的长度,就不放代码了。
P1547:AC 记录
关于 P1888:闲来无事刷刷水题
P1888:AC 记录
P2367 语文成绩
复习一下差分,做一做差分的模板题。
差分数组记录每次修改,最终统一遍历,判断求最小值即可。
#include<iostream>
using namespace std;
int n,p,a[5000005],c[5000005],minn=2147483647,ans=0;
int main(){
cin>>n>>p;
for(int i=1;i<=n;i++)cin>>a[i];
while(p--){
int l,r,s;
cin>>l>>r>>s;
c[l]+=s;
c[r+1]-=s;
}
for(int i=1;i<=n;i++){
ans+=c[i];
if(a[i]+ans<minn)minn=a[i]+ans;
}
cout<<minn;
return 0;
}
P2367:AC 记录
SP2 PRIME1 - Prime Generator
一道基础的素数筛。
#include<iostream>
using namespace std;
bool sss(unsigned long long n){
if(n==2)return 1;
if(n<2||n%2==0)return 0;
else{
for(unsigned long long i=3;i*i<=n;i+=2){
if(!(n%i))return 0;
}
return 1;
}
}
int main(){
int t;
cin>>t;
while(t--){
unsigned long long n,m;
cin>>m>>n;
for(unsigned long long i=m;i<=n;i++){
if(sss(i))cout<<i<<'\n';
}
cout<<'\n';
}
return 0;
}
SP2:AC 记录
8.7
-
P4305 [JLOI2011] 不重复数字
-
SP34013 SEUG - Seetha’s Unique Game
-
UVA12289 One-Two-Three
-
B3946 [语言月赛 202403] 2014
-
P10728 [NOISG 2023 Qualification] Swords
-
P10523 [XJTUPC2024] Everyone's ALL IN
-
P1754 球迷购票问题
P4305 [JLOI2011] 不重复数字
给定
本题直接模拟存储肯定会 TLE 或者 MLE,因此我们正好可以复习一下昨天学的 map。
用 map 存储每个元素是否出现过,如果是则不记录该元素,同时让
#include<iostream>
#include<map>
using namespace std;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
long long n,a[50005];
map<long long,bool> p;
p.clear();
cin>>n;
for(int i=0;i<n;i++){
long long l;
cin>>l;
if(p[l]){
n--;
i--;
}
else{
a[i]=l;
p[l]=1;
}
}
for(int i=0;i<n;i++)cout<<a[i]<<' ';
cout<<'\n';
}
return 0;
}
P4305:AC 记录
P10728 [NOISG 2023 Qualification] Swords
本题的坑点在于要特判有一种能力相同的情况,为此我们可以定义一个 cmp。先严格按照一种能力比如攻击力排序,然后判断防御力是否比所有攻击力比它高的剑都强,即用一个 maxn 来记录最大值,不断更新即可。
#include<bits/stdc++.h>
using namespace std;
int n,maxn,s;
struct sw{
int a,b;
}a[100001];
bool cmp(sw a,sw b){
if(a.a==b.a)return a.b<b.b;
else return a.a<b.a;
}
int main(){
cin>>n;
for(int i=0;i<n;i++)cin>>a[i].a>>a[i].b;
sort(a,a+n,cmp);
for(int i=n-1;i>=0;i--){
if(a[i].b>maxn){
maxn=a[i].b;
s++;
}
}
cout<<s;
return 0;
}
P10728:AC 记录
P1754 球迷购票问题
一道 dp 题。
- C++ 中是有自带的求最小公约数的函数的,即
__gcd()
所以问题就被轻松的解决了。不过记得开 long long。
B3634:AC 记录
[P8598 [蓝桥杯 2013 省 AB] 错误票据
给你一串只有一处间断和一处重复的连续数字序列,让你求出断号和重号的位置。
考虑使用桶排,另外这里的 cin 可以一直读入。
#include<iostream>
using namespace std;
int n,a[100005],mn=100005,d,c;
int main(){
cin>>n;
int i;
while(cin>>i){
a[i]++;
mn=min(mn,i);
}
for(int i=mn;!(d&&c);i++){
if(!a[i])d=i;
if(a[i]>1)c=i;
}
cout<<d<<' '<<c;
return 0;
}
P8598:AC 记录
B2107 图像旋转
忌:切水题
我偏切
输入一个
#include<iostream>
using namespace std;
int main(){
int n,m,a[101][101];
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)cin>>a[i][j];
}
for(int i=0;i<m;i++){
for(int j=n-1;j>=0;j--)cout<<a[j][i]<<' ';
cout<<'\n';
}
return 0;
}
B2107:AC 记录
B3614 【模板】栈
一道栈的模板模拟题很好,使我的代码旋转。
坑死我了。
坑死我了坑死坑死我了我坑坑死我了死坑死坑死死我了我坑死我了坑死我了了坑死了坑死我我了坑死我了坑死坑死我了我了坑死我了坑坑死我了死我坑死我了坑我了我了了。
不放代码了,等着你们去踩坑。
B3614:AC 记录
B3616 【模板】队列
正常的模板题,一遍过。
#include<bits/c++.h>
using namespace std;
int main(){
queue<int> q;
cin>>n;
while(n--){
cin>>s;
if(s==1){
int x;
cin>>x;
q.push(x);
}
else if(s==2){
if(q.empty())cout<<"ERR_CANNOT_POP";
else q.pop();
}
else if(s==3){
if(q.empty())cout<<"ERR_CANNOT_QUERY";
else cout<<q.front();
}
else cout<<q.size();
}
return 0;
}
(已防抄袭)
B3616:AC 记录
P1028 [NOIP2001 普及组] 数的计算
很简单的一道递归。
边界是
#include<iostream>
using namespace std;
int n,s,f[2024];
int fv(int n){
if(f[n])return f[n];
f[n]=1;
for(int i=1;i*2<=n;i++)f[n]+=fv(i);
return f[n];
}
int main(){
f[1]=1;
cin>>n;
cout<<fv(n);
return 0;
}
P1028:AC 记录
P1947 猜数
终于学会写交互题了,好开心
典型二分,洛谷给我们这道题主要是让用户学会使用交互题。
#include<cstdio>
#include<iostream>
extern "C" int Seniorious(int);
extern "C" int Chtholly(int n, int OvO){
for(int l=1,r=n,mid=(l+r)>>1,ans;l<=r;mid=(l+r)>>1){
ans=Seniorious(mid);
if(ans==0)return mid;
else if(ans==-1)l=mid+1;
else r=mid-1;
}
}
P1947:AC 记录
8.11
-
P7225 [RC-04] 走迷宫
-
P4279 [SHOI2008] 小约翰的游戏
-
P2884 [USACO07MAR] Monthly Expense S
-
UVA10810 Ultra-QuickSort
-
ATutpc2011_7 プログラミングコンテストチャレンジブック
-
P5412 [YNOI2019] 排队
P7225 [RC-04] 走迷宫
一道交互模式下的 dfs。用记忆化提升效率,每次走完再回去就好。
#include<bits/stdc++.h>
using namespace std;
int f[505][505],dx[]={-1,0,1,0},dy[]={0,-1,0,1};
char k[4]={'W','A','S','D'};
extern "C" bool move_to(char position);
void dfs(int x,int y){
f[x][y]=1;
for(int i=0;i<4;i++){
int xa=dx[i]+x,ya=dy[i]+y;
if(~f[xa][ya])continue;
bool b=move_to(k[i]);
if(!b)f[xa][ya]=0;
else{
dfs(xa,ya);
if(i%2==1)move_to(k[4-i]);
else move_to(k[2-i]);
}
}
return ;
}
extern "C" string find_out_map(int x,int y,int n){
string s;
dfs(x,y);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
s+=((f[i][j]==1)?'0':'1');
}
}
return s;
}
P7225:AC 记录
P4279 [SHOI2008] 小约翰的游戏
找一道博弈论蓝题练练手,要特判堆数全为
P4279:AC 记录
AT utpc2011_7 プログラミングコンテストチャレンジブック
一道简单的贪心。
#include<bits/stdc++.h>
using namespace std;
int n,top=0,a[100000],f[100000],ans=0,ansone,anstwo=0;
int main(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+1+n);
for(int i=3;i<=n;i++)if(a[i]<a[i-2]+a[i-1])f[++top]=i;
ansone=f[top];
for(int i=1;i<top;i++)if(f[top]-f[i]>=3)anstwo=f[i];
if(ansone>=6){
int t1=a[f[top]-5],t2=a[f[top]-4],t3=a[f[top]-3],t4=a[f[top]-2],t5=a[f[top]-1],t6=a[f[top]],tsum=t1+t2+t3+t4+t5+t6;
if(一堆情况)ans=tsum;
}
if(!ans&&anstwo)ans=a[ansone]+a[ansone-1]+a[ansone-2]+a[anstwo]+a[anstwo-1]+a[anstwo-2];
cout<<ans<<'\n';
AT utpc2011_7:AC 记录
P5412 [YNOI2019] 排队
一开始把 YNOI 认成 Ynoi 了
一道简单的排序。
#include<iostream>
#include<algorithm>
int n,t;
bool a[2001];
double n1[2001],n2[2001];
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
int i1=0,i2=0;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++){
double s;
cin>>s;
if(a[i])n1[++i1]=s;
else n2[++i2]=s;
}
sort(n1+1,n1+i1+1);
sort(n2+1,n2+i2+1);
for(int i=1;i<=i2;i++)cout<<n2[i]<<' ';cout<<'\n';
for(int i=1;i<=i1;i++)cout<<n1[i]<<' ';cout<<'\n';
}
return 0;
}
P5412:AC 记录
8.12~8.16
-
P10862 [HBCPC2024] Spicy or Grilled?
-
B3874 [GESP202309 六级] 小杨的握手问题
-
AT_arc026_4 [ARC026D] 道を直すお仕事
-
P1577 切绳子
-
P4702 取石子
-
B3989 [语言月赛 202406] 优惠券
博弈论专题
-
P2197 【模板】Nim 游戏
-
P4279 [SHOI2008] 小约翰的游戏
-
P10398 『STA - R5』Remove and Decrease Game
-
P3235 [HNOI2014] 江南乐
8.16
-
AT_atc002_b n^p mod m
-
P10877 「KDOI-07」n1gr tS0i
-
P2309 loidc,卖卖萌
-
P2310 loidc,看看海
-
UVA113 Power of Cryptography
P2309 loidc,卖卖萌
这就是一个正序对问题,用归并把逆序对的方法改一下即可。
P2309:AC 记录
AT_atc002_b n^p mod m
给你
典型的快速幂。
#include<iostream>
using namespace std;
long long p;
long long qsm(long long a,long long b){
if(b==0)return 1;
long long t=qsm(a,b/2);
if(b&1)return t*t%p*a%p;
return t*t%p;
}
int main(){
long long a,b;
cin>>a>>p>>b;
cout<<qsm(a,b)<<endl;
return 0;
}
AT_atc002:AC 记录
UVA113 Power of Cryptography
一个 pow 函数的应用,注意控制精度。
#include<bits/stdc++.h>
using namespace std;
int main(){
double n,p;
while(cin>>n>>p)printf("%.0lf\n",pow(p,1.0/n));
return 0;
}
UVA113:AC 记录
2024 -- 星航计划 -- 八月份 -- CSP-J全真模拟赛
here
8.17
-
P1181 数列分段 Section I
-
P10720 [GESP202406 五级] 小杨的幸运数字
-
AT_tenka1_2014_qualA_a 天下一序数
-
AT_arc026_2 [ARC026B] 完全数
-
UVA136 丑数 Ugly Numbers
P1181 数列分段 Section I
简单的贪心。
#include<iostream>
using namespace std;
int main(){
int n,m,a,sum=0,ans=0;
cin>>n>>m;
while(n--){
cin>>a;
if(sum+a>m){
ans++;
sum=a;
t=0;
}
else sum+=a;
t++;
}
cout<<((t)?ans+1:ans);
return 0;
}
P1181:AC 记录
P10720 [GESP202406 五级] 小杨的幸运数字
简单的数论。依次从小到大枚举质因数即可。
#include<iostream>
using namespace std;
int f(int n){
int s=0,N=n;
for(int i=2;i*i<=N;i++){
if(!(n%i)){
s++;
while(!(n%i))n/=i;
}
}
return ((n>1)?s+1:s);
}
int main(){
int n;
cin>>n;
while(n--){
int a;
cin>>a;
cout<<(f(a)==2)<<'\n';
}
return 0;
}
P10720:AC 记录
AT_tenka1_2014_qualA_a 天下一序数
一道简单的提交答案题
将
AT_tenka1_2014_qualA_a:AC 记录
AT_arc026_2 [ARC026B] 完全数
简单的完全数判断,直接遍历
另外,要特判
#include<iostream>
using namespace std;
long long f(long long n){
long long s=1;
for(long long i=2;i*i<=n;i++){
if(n%i==0){
if(i==n/i)s+=i;
else s+=i+n/i;
}
}
return s;
}
int main(){
long long n,t;
cin>>n;
t=f(n);
if(n==1)cout<<"Deficient\n";
else if(t==n)cout<<"Perfect\n";
else if(t<n)cout<<"Deficient\n";
else cout<<"Abundant\n";
return 0;
}
AT_arc026_2:AC 记录
8.18
最后一天了。
-
AT_abc008_2 [ABC008B] 投票
-
UVA10340 子序列 All in All
-
UVA12626 I ❤ Pizza
-
P10291 [CCC 2024 J2] Dusa And The Yobis
-
P6101 [EER2] 出言不逊
-
P1597 语句解析
AT_abc008_2 [ABC008B] 投票
某个组织进行了选拔领导人的选举。组织由 n 人组成,每个人都写了最领导的人物的名字。投票数最多的人的名字就是领袖。如果有多个得票数最多的人物,输出其中一个即可。 不就是基础的 map 应用吗?
#include<bits/stdc++.h>
using namespace std;
map<string,int>p;
int n,maxn;
int main(){
string s,maxa;
cin>>n;
while(n--){
cin>>s;
if(!p.count(s))p[s]=1;
else p[s]++;
if(p[s]>maxn){
maxn=p[s];
maxa=s;
}
}
cout<<maxa<<endl;
return 0;
}
AT_abc008_2:AC 记录
UVA10340 子序列 All in All
跟十天前搬的 T493180 目标字符串 好像啊。
queue<char> q;
string x,y;
while(cin>>x>>y){
while(!q.empty())q.pop();
for(int i=0;i<x.size();i++)q.push(x[i]);
for(int i=0;i<y.size();i++){
if(y[i]==q.front())q.pop();
}
cout<<((q.empty())?"Yes\n":"No\n");
}
UVA10340:AC 记录
P6101 [EER2] 出言不逊
又是一道陈年老 TLE 题。原来是 unsigned long long 会溢出,用 int128 就好了。
#include<bits/stdc++.h>
#define int __uint128_t
using namespace std;
int a[114514],maxi,sum,siz;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
int l,maxn=0;
unsigned long long L;
cin>>s>>L;
l=L;
if(s.size()>=l){
cout<<(unsigned long long)sum;
return 0;
}
for(int i=0;i<s.size();i++){
a[s[i]-48]++;
if(s[i]-48>maxi)maxi=s[i]-48;
}
for(int i=0;i<=maxi;i++){
if(a[i]>maxn)maxn=a[i];
}
siz=s.size();
while(siz<l){
siz+=maxn;
maxn*=2;
sum++;
}
cout<<(unsigned long long)sum;
return 0;
}
P6101:AC 记录
upd:2024.8.18 23:17
总结
一共做题