第二十六天 2021-4-10 备战CSP
刷题模块:CSP 201703-1-2-3-4
最后一天复习CSP,明天正式考试。
之后将降低算法学习强度,每周学习一章AcWing教程即可。
一、201703-1
Easy.
#include<iostream>
#include<cstdio>
using namespace std;
int n,k,w;
int sumW,sumP;
int main(){
sumP=0;sumW=0;
scanf("%d %d",&n,&k);
while(n--){
scanf("%d",&w);
sumW+=w;
if(sumW>=k){
sumP++;
sumW=0;
}
}
if(sumW) sumP++;
printf("%d\n",sumP);
return 0;
}
二、201703-2
Easy.
双向链表
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1010;
int l[N],r[N],h;
int n,m;
int stu,dis;
void init(){
h=0;
//node 0 is a head node, node n+1 is a tail node
for(int i=0;i<=n+1;i++){
r[i]=i+1;
l[i]=i-1;
}
}
void reStu(int u){
if(u==0 || u==n+1) printf("Error\n");
r[l[u]]=r[u];
l[r[u]]=l[u];
}
void addStu(int b,int u){
if(b==n+1) printf("Error\n");
l[u]=b,r[u]=r[b];
l[r[b]]=u,r[b]=u;
}
void moveStu(int stu,int dis){
int b=stu;
reStu(stu);
if(dis>0){
while(dis--){
b=r[b];
}
addStu(b,stu);
}
else{
while(dis++){
b=l[b];
}
addStu(l[b],stu);
}
}
int main(){
scanf("%d %d",&n,&m);
init();
while(m--){
scanf("%d %d",&stu,&dis);
moveStu(stu,dis);
}
for(int i=r[h];i!=n+1;i=r[i]){
printf("%d ",i);
}
puts("");
}
三、201703-3
算法是弱项,大模拟是强项,先做第三题
一遍过,爽
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
void transLine(string& line);
bool isParg;
string bt[7]={"","<h1>","<h2>","<h3>","<h4>","<h5>","<h6>"};
string et[7]={"","</h1>","</h2>","</h3>","</h4>","</h5>","</h6>"};
string bl[2]={"<ul>","<li>"};
string el[2]={"</ul>","</li>"};
string p[2]={"<p>","</p>"};
string em[2]={"<em>","</em>"};
string a[3]={"<a href=\"","\">","</a>"};
void transBlock(string& l){
//title processing
if(l[0]=='#'){
int i=1;
while(l[i]=='#') {i++;}
l.erase(0,i+1);
transLine(l);
l.insert(0,bt[i]);
l.insert(l.size(),et[i]);
cout<<l<<endl;
}
//list processing
else if(l[0]=='*'){
cout<<bl[0]<<endl;
do{
l.erase(0,2);
transLine(l);
l.insert(0,bl[1]);
l.insert(l.size(),el[1]);
cout<<l<<endl;
}while(getline(cin,l) && l[0]=='*');
cout<<el[0]<<endl;
}
//paragraph processing
else{
transLine(l);
l.insert(0,p[0]);
string nl;
while(getline(cin,nl) && nl!=""){
transLine(l);
cout<<l<<endl;
l=nl;
}
transLine(l);
l.insert(l.size(),p[1]);
cout<<l<<endl;
}
}
void transLine(string& l){
int i=0,j=0,k;
//deal with em
while(i<l.size()){
while(l[i]!='_' && i<l.size()){i++;}
if(i!=l.size()){
l.erase(i,1);
l.insert(i,em[j]);
j=(j+1)%2;
i++;
}
}
//deal with link
i=0;
string text,link;
while(i<l.size()){
while(l[i]!='[' && i<l.size()){i++;}
if(i!=l.size()){
j=i+1;
while(l[j]!=']'){j++;}
text=l.substr(i+1,j-i-1);
k=++j;
while(l[k]!=')'){k++;}
link=l.substr(j+1,k-j-1);
l.erase(i,k-i+1);
string temp=a[0]+link+a[1]+text+a[2];
l.insert(i,temp);
}
}
}
int main(){
freopen("in.txt","r",stdin);
string line;
while(getline(cin,line)){
if(line=="") continue;
transBlock(line);
}
return 0;
}
四、201703-4
评测一遍过,考试前一天,还是要稳住心态
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100010,M=N*2;
struct Edge{
int a,b,c;
Edge():a(0),b(0),c(0){}
Edge(int a,int b,int c):a(a),b(b),c(c){}
bool operator<(const Edge& e)const{
return c<e.c;
}
}e[M];
int n,m;
int idx,p[N];
bool st[N];
int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
int Kruskal(){
int i=0;
int maxL;
while(find(1)!=find(n)){
int a=e[i].a;
int b=e[i].b;
int x=find(a),y=find(b);
if(x==y){i++;continue;}
else{
p[x]=y;
maxL=e[i].c;
i++;
}
}
return maxL;
}
int main(){
idx=0;
memset(st,0,sizeof st);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
p[i]=i;
}
for(int i=0;i<m;i++){
scanf("%d %d %d",&e[i].a,&e[i].b,&e[i].c);
}
sort(e,e+m);
printf("%d\n",Kruskal());
return 0;
}