#include<iostream>#include<vector>#include<algorithm>usingnamespace::std;struct action {bool is_return;int num;int time;action(constbool&x,constint&n,constint&y):is_return(x),num(n),time(y){};};classpublicKeys{public:
vector<int>allkeys;publicKeys(int n){for(int i =1; i <= n; i++){
allkeys.push_back(i);}}voidtake(action a){for(int i =0; i < allkeys.size(); i++){if(allkeys[i]== a.num){
allkeys[i]=0;break;}}}voidreturn_key(action b){for(int i =0; i < allkeys.size(); i++){if(allkeys[i]==0){
allkeys[i]= b.num;break;}}}voidprint(){for(int i =0; i < allkeys.size(); i++){
cout<<allkeys[i]<<" ";}
cout<<endl;}};boolsortFun(const action &a1,const action &a2){if(a1.time == a2.time){if(a1.is_return &&!a2.is_return){returntrue;}if(!a1.is_return && a2.is_return){returnfalse;}if(a1.is_return && a2.is_return){return a1.num < a2.num;}}return a1.time < a2.time;//升序排列 }intmain(){int N, K;
cin >> N >> K;
publicKeys p =publicKeys(N);
vector<action>actions;for(int i =0; i < K; i++){int num, start, duration;
cin >> num >> start >> duration;
action a =action(false, num, start);
action b =action(true, num, start + duration);
actions.push_back(a);
actions.push_back(b);}sort(actions.begin(), actions.end(), sortFun);// for (int i = 0; i < actions.size(); i++) {// cout<<actions[i].num<< " " << actions[i].is_return<< " " << actions[i].time<<endl;// }for(int i =0; i < K*2; i++){if(actions[i].is_return){//cout<<"ready to return "<<endl;
p.return_key(actions[i]);//p.print();}else{//cout << "ready to take"<<endl;
p.take(actions[i]);//p.print();}}
p.print();return0;}
第四题:通信网络
#include<iostream>#include<vector>#include<stdlib.h>#include<string.h>usingnamespace::std;int M,N;
vector<int>Graph[1005];int visit[1005];bool reach[1005][1005]={0};voiddfs(int v,int start){
visit[v]=1;
reach[v][start]= reach[start][v]=true;for(int i =0; i < Graph[v].size(); i++){if(!visit[Graph[v][i]]){dfs(Graph[v][i], start);}}}intmain(){
cin >> N >> M;for(int i =0; i < M; i++){int u, v;
cin >> u >> v;
Graph[u].push_back(v);}for(int i =1; i <= N; i++){memset(visit,0,sizeof(visit));dfs(i, i);}int res =0;for(int i =1; i <= N; i++){int j;for(j =1; j <= N; j++){if(!reach[i][j])break;}//cout<<"J is : " << j << " "<<endl;if(j == N +1)res++;}
cout << res<<endl;}
第五题:除法
#include<iostream>#include<vector>usingnamespace::std;intmain(){int N, M;
cin >> N >> M;
vector<int>numbers;for(int i =0; i < N; i++){int k;
cin >> k;
numbers.push_back(k);}for(int i =0; i < M; i++){int opt;
cin >> opt;if(opt ==2){int l, r;
cin >> l >> r;int res =0;for(int j = l -1; j < r; j++){
res += numbers[j];}
cout << res<<endl;}if(opt ==1){int l, r, v;
cin >> l >> r >> v;for(int j = l -1; j < r; j++){if(numbers[j]% v ==0){
numbers[j]= numbers[j]/v;}}}}return0;}