第一题 7-1 Arithmetic Progression of Primes (20 分)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
bool primer[N];
int n,p;
void sushu() {
fill(primer,primer+p+1,true);
primer[0]=primer[1]=false;
for(int i=2;i<=p;++i) {
if(primer[i]==false) continue;
for(int j=i*2;j<=p;j+=i) {
primer[j]=false;
}
}
}
bool check(int a, int b) {
int cnt=0;
while(primer[a]) {
++cnt;
if(cnt==n) return true;
a-=b;
if(a<0) break;
}
return false;
}
int main() {
scanf("%d %d",&n,&p);
sushu();
if(n==1) {
int res=n;
for(int i=p;i>1;--i) {
if(primer[i]) {
res=i;
break;
}
}
printf("%d",res);
return 0;
}
int d=p/(n-1);
int x=-1,y=-1;
bool flag=false;
for(int j=d;j>=1;--j) {
for(int i=p;i>=j*(n-1)+2;--i) {
if(primer[i]==false) continue;
if(check(i,j)) {
x=i;
y=j;
flag=true;
break;
}
}
if(flag) break;
}
if(flag) {
for(int i=x-(n-1)*y;i<=x;i+=y) {
printf("%d",i);
if(i!=x) printf(" ");
}
} else {
int res=n;
for(int i=p;i>1;--i) {
if(primer[i]) {
res=i;
break;
}
}
printf("%d",res);
}
return 0;
}
![](https://img2020.cnblogs.com/blog/1143334/202103/1143334-20210316223516174-1927959716.png)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 100005;
int n,p;
int len;
bool isprimer[N];
vector<int> primer,res;
void prime() {
fill(isprimer,isprimer+p+1,true);
isprimer[0]=isprimer[1]=false;
for(int i=2;i<=p;++i) {
if(isprimer[i]) {
int j=i*2;
while(j<=p) isprimer[j]=false,j+=i;
primer.push_back(i);
}
}
return;
}
int main() {
scanf("%d %d",&n,&p);
prime();
len=primer.size();
if(n==1) {
printf("%d",primer[len-1]);
return 0;
}
int d=p/(n-1);
for(int j=d;j>=1;--j) {
for(int i=len-1;i>=0&&primer[i]>=(n-1)*j+2;--i) {
res.clear();
for(int k=n-1;k>=0;--k) {
if(isprimer[primer[i]-k*j]) {
res.push_back(primer[i]-k*j);
} else break;
}
if(res.size()==n) break;
}
if(res.size()==n) break;
}
if(res.size()==n) {
for(int i=0;i<n;++i) {
printf("%d",res[i]);
if(i!=n-1) printf(" ");
}
} else printf("%d",primer[len-1]);
return 0;
}
![](https://img2020.cnblogs.com/blog/1143334/202103/1143334-20210316223533649-1676390800.png)
第二题 7-2 Lab Access Scheduling (25 分)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 2005;
struct node {
int st,ed;
};
node ans[N];
int n,hh,mm,ss;
bool cmp(node &a, node &b) {
if(a.st!=b.st) return a.st<b.st;
return a.ed<b.ed;
}
int main() {
scanf("%d",&n);
for(int i=0;i<n;++i) {
scanf("%d:%d:%d",&hh,&mm,&ss);
ans[i].st=hh*3600+mm*60+ss;
scanf("%d:%d:%d",&hh,&mm,&ss);
ans[i].ed=hh*3600+mm*60+ss;
}
sort(ans,ans+n,cmp);
int res=1,pos=0;
for(int i=1;i<n;++i) {
if(ans[i].st>=ans[pos].ed) ++res,pos=i;
else {
if(ans[i].ed<=ans[pos].ed) pos=i;
}
}
cout<<res;
return 0;
}
![](https://img2020.cnblogs.com/blog/1143334/202103/1143334-20210316223559579-1453064425.png)
第三题 7-3 Structure of Max-Heap (25 分)
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <map>
#include <unordered_map>
using namespace std;
const int N = 10005;
int n,m,a,b;
string s;
int ans[N],res[25];
unordered_map<int,int> mp;
int main() {
cin>>n>>m;
for(int i=1;i<=n;++i) {
cin>>ans[i];
int j=i;
while(ans[j]>ans[j>>1]&&j>=2) {
swap(ans[j],ans[j>>1]);
j>>=1;
}
}
for(int i=1;i<=n;++i) mp.insert(make_pair(ans[i],i));
for(int i=1;i<=m;++i) {
cin>>a;
cin>>s;
if(s=="and") {
cin>>b;
if(mp[a]/2==mp[b]/2) res[i]=1;
else res[i]=0;
cin>>s;
cin>>s;
} else {
cin>>s;
cin>>s;
if(s=="parent") {
cin>>s;
cin>>b;
if(mp[b]/2==mp[a]) res[i]=1;
else res[i]=0;
} else if(s=="left") {
cin>>s;cin>>s;cin>>b;
if(ans[mp[b]*2]==a) res[i]=1;
else res[i]=0;
} else if(s=="right") {
cin>>s;cin>>s;cin>>b;
if(ans[mp[b]*2+1]==a) res[i]=1;
else res[i]=0;
} else if(s=="root") {
if(mp[a]==1) res[i]=1;
else res[i]=0;
}
}
}
for(int i=1;i<=m;++i) cout<<res[i];
return 0;
}
![](https://img2020.cnblogs.com/blog/1143334/202103/1143334-20210316223614069-2003424117.png)
第四题 7-4 Recycling of Shared Bicycles (30 分)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 210;
const int INF = 0x3f3f3f3f;
int n,m,sum,cnt;
int G[N][N],res[N];
bool vis[N];
void floyd() {
for(int k=0;k<=n;++k) {
for(int i=0;i<=n;++i) {
for(int j=0;j<=n;++j) {
if(G[i][k]!=INF&&G[k][j]!=INF&&G[i][k]+G[k][j]<G[i][j]) {
G[i][j]=G[j][i]=G[i][k]+G[k][j];
}
}
}
}
}
int main() {
fill(G[0],G[0]+N*N,INF);
fill(vis,vis+N,false);
scanf("%d %d",&n,&m);
for(int i=0;i<=n;++i) G[i][i]=0;
int u,v,d;
for(int i=0;i<m;++i) {
scanf("%d %d %d",&u,&v,&d);
G[u][v]=G[v][u]=d;
}
floyd();
u=0;
vis[0]=true;
res[0]=0;
cnt=1;
for(int i=1;i<=n;++i) {
int mindis=INF;
v=INF;
for(int j=1;j<=n;++j) {
if(j==u) continue;
if(!vis[j]&&G[u][j]<mindis) {
v=j;
mindis=G[u][j];
}
}
if(v==INF) {
break;
} else {
sum+=G[u][v];
vis[v]=true;
res[cnt++]=v;
u=v;
}
}
for(int i=0;i<cnt;++i) {
printf("%d",res[i]);
if(i!=cnt-1) printf(" ");
else printf("\n");
}
if(cnt==n+1) {
printf("%d",sum);
} else {
cnt=0;
for(int i=0;i<=n;++i) {
if(vis[i]==false) {
res[cnt++]=i;
}
}
for(int i=0;i<cnt;++i) {
printf("%d",res[i]);
if(i!=cnt-1) printf(" ");
}
}
return 0;
}
![](https://img2020.cnblogs.com/blog/1143334/202103/1143334-20210316223638435-759970616.png)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 210;
const int INF = 0x3f3f3f3f;
int n,m,cnt,sum;
int G[N][N];
int dis[N],res[N];
bool vis[N],check[N];
void dij(int s) {
fill(vis,vis+N,false);
fill(dis,dis+N,INF);
dis[s]=0;
for(int i=0;i<=n;++i) {
int v=-1,tempdis=INF;
for(int j=0;j<=n;++j) {
if(!vis[j]&&dis[j]<tempdis) {
v=j;
tempdis=dis[j];
}
}
if(v==-1) break;
vis[v]=true;
for(int u=0;u<=n;++u) {
if(G[v][u]!=INF&&dis[v]+G[v][u]<dis[u]) {
dis[u]=dis[v]+G[v][u];
}
}
}
return;
}
int main() {
scanf("%d %d",&n,&m);
fill(G[0],G[0]+N*N,INF);
fill(check,check+N,false);
for(int i=0;i<=n;++i) G[i][i]=0;
int u,v,d;
for(int i=0;i<m;++i) {
scanf("%d %d %d",&u,&v,&d);
G[u][v]=G[v][u]=d;
}
u=0;
check[0]=true;
res[0]=0;
++cnt;
for(int i=0;i<=n;++i) {
dij(u);
v=-1;
int tempdis=INF;
for(int j=0;j<=n;++j) {
if(j==u) continue;
if(!check[j]&&dis[j]<tempdis) {
tempdis=dis[j];
v=j;
}
}
if(v==-1) break;
u=v;
sum+=tempdis;
res[cnt++]=v;
check[v]=true;
}
for(int i=0;i<cnt;++i) {
printf("%d",res[i]);
if(i!=cnt-1) printf(" ");
else printf("\n");
}
if(cnt==n+1) {
printf("%d",sum);
} else {
cnt=0;
for(int i=0;i<=n;++i) {
if(!check[i]) res[cnt++]=i;
}
for(int i=0;i<cnt;++i) {
printf("%d",res[i]);
if(i!=cnt-1) printf(" ");
}
}
return 0;
}
![](https://img2020.cnblogs.com/blog/1143334/202103/1143334-20210316223650038-1039285114.png)