PAT甲级题解分类byZlc

时间:2021-09-27 09:22:36

专题一  字符串处理

A1001 Format(20)

#include<cstdio>
int main () {
int s[];
int a,b,sum;
scanf ("%d %d",&a,&b);
sum=a+b;
if (sum<) {
printf ("-");
sum=-sum;
}
int z,top=;
if (sum==) {
s[top++]=;
}
while (sum!=) { s[top++]=sum%;
sum/=;
} for (int i=top-;i>=;i--) {
printf ("%d",s[i]); if (i%==&&i!=) printf (",");
}
return ;
}

A1005 Spell It Right(20)

#include<cstdio>
int main () {
char s[][]={"zero","one","two","three","four","five","six","seven","eight","nine"};
char q[];
int w=;
while ((q[w]=getchar())!='\n')
w++;
q[w]='\0';
int a[],sum=;
for (int i=;i<w;i++)
a[i]=q[i]-'';
for (int i=;i<w;i++)
sum+=a[i];
int z[],top;
while (sum!=) {
z[top++]=sum%;
sum/=;
}
printf ("%s",s[z[top-]]);
for (int i=top-;i>=;i--) {
printf (" %s",s[z[i]]);
}
return ;
}

A1023 Have Fun with Numbers(20)

#include<cstdio>
#include<cstring>
using namespace std;
int book[];
int main () {
char num[];
scanf ("%s",num);
int flag=,len=strlen(num);
for (int i=len-;i>=;i--) {
int temp=num[i]-'';
book[temp]++;
temp=temp*+flag;
flag=;
if (temp>=) {
temp=temp-;
flag=;
}
num[i]=temp+'';
book[temp]--;
}
int flag1=;
for (int i=;i<;i++)
if (book[i]!=) flag1=;
printf ("%s",(flag==||flag1==)?"No\n":"Yes\n");
if (flag==) printf ("");
printf ("%s",num);
return ;
}

A1077 Kuchiguse(20)

#include<cstdio>
#include<cstring>
int n,minLen=,ans=;
char s[][];
int main () {
scanf ("%d",&n);
getchar ();
for (int i=;i<n;i++) {
int w=;
while ((s[i][w]=getchar())!='\n')
w++;
s[i][w]='\0';
int len=strlen(s[i]);
if (len<minLen) minLen=len;
for (int j=;j<len/;j++) {
char temp=s[i][j];
s[i][j]=s[i][len-j-];
s[i][len-j-]=temp;
}
}
for (int i=;i<minLen;i++) {
char c=s[][i];
bool same=true;
for (int j=;j<n;j++) {
if (c!=s[j][i]) {
same=false;
break;
}
}
if (same) ans++;
else break;
}
if (ans) {
for (int i=ans-;i>=;i--)
printf ("%c",s[][i]);
}
else printf ("nai");
return ;
}

专题二  树的遍历

A1004 Counting Leaves(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int> g[maxn];
int d[maxn];
int maxdepth=-;
void dfs (int root,int depth) {
maxdepth=max(maxdepth,depth);
if (g[root].size()==) d[depth]++;
for (int i=;i<g[root].size();i++)
dfs (g[root][i],depth+);
}
int main () {
int N,M;
scanf ("%d %d",&N,&M);
int num,k,x;
for (int i=;i<M;i++) {
scanf ("%d %d",&num,&k);
for (int j=;j<k;j++) {
scanf ("%d",&x);
g[num].push_back(x);
}
}
dfs (,);
for (int i=;i<=maxdepth;i++) {
if (i!=) printf (" ");
printf ("%d",d[i]);
}
return ;
}

A1053 Path of Equal Weight(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
vector<int> child;
}Node[maxn];
vector<int> path;
int N,M,S;
bool cmp (int a,int b) {
return Node[a].data>Node[b].data;
}
void dfs (int root,int sum) {
path.push_back(root);
if (Node[root].child.size()==) {
if (sum==S) {
for (int i=;i<path.size();i++) {
if (i!=) printf (" ");
printf ("%d",Node[path[i]].data);
}
printf ("\n");
}
path.pop_back();
return;
}
for (int i=;i<Node[root].child.size();i++)
dfs (Node[root].child[i],sum+Node[Node[root].child[i]].data);
path.pop_back();
return;
}
int main () {
scanf ("%d %d %d",&N,&M,&S);
for (int i=;i<N;i++) {
scanf ("%d",&Node[i].data);
}
int num,k,x;
for (int i=;i<M;i++) {
scanf ("%d %d",&num,&k);
for (int j=;j<k;j++) {
scanf ("%d",&x);
Node[num].child.push_back(x);
}
sort (Node[num].child.begin(),Node[num].child.end(),cmp);
}
dfs (,Node[].data);
return ;
}

A1079 Total Sales of Supply Chain(25)

#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
int n;
double p,r;
struct node {
int data;
vector<int> child;
}Node[maxn];
double sum=;
void DFS (int root,int depth) {
if (Node[root].child.size()==) {
sum+=Node[root].data*p*pow(+r,depth);
return;
}
for (int i=;i<Node[root].child.size();i++)
DFS (Node[root].child[i],depth+);
}
int main () {
scanf ("%d%lf%lf",&n,&p,&r);
r/=;
int k,child;
for (int i=;i<n;i++){
scanf ("%d",&k);
if (k==) scanf ("%d",&Node[i].data);
else {
for (int j=;j<k;j++) {
scanf ("%d",&child);
Node[i].child.push_back(child);
}
}
}
DFS(,);
printf ("%.1f",sum);
}

A1090 Highest Price in Supply Chain(25)

#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=;
vector<int> Node[maxn];
double highest=;
int highestnum=;
double price;
int n;
double p,r;
void DFS (int root,int depth) {
if (Node[root].size()==) {
price=p*pow(+r,depth);
if (price>highest) {
highest=price;
highestnum=;
}
else if (price==highest) {
highestnum++;
}
return;
}
for (int i=;i<Node[root].size();i++)
DFS (Node[root][i],depth+);
}
int main () {
scanf ("%d%lf%lf",&n,&p,&r);
r/=;
int root;
int father;
for (int i=;i<n;i++) {
scanf ("%d",&father);
if (father==-) root=i;
else Node[father].push_back(i);
}
DFS (root,);
printf ("%.2f %d",highest,highestnum);
return ;
}

A1094 The Largest Generation(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
vector<int> child;
}Node[maxn];
int d[maxn]={},maxDepth=-;
void dfs (int root,int depth) {
d[depth]++;
maxDepth=max(depth,maxDepth);
for (int i=;i<Node[root].child.size();i++)
dfs (Node[root].child[i],depth+);
}
int main () {
int N,M,K;
int num,x;
scanf ("%d %d",&N,&M);
for (int i=;i<M;i++) {
scanf ("%d %d",&num,&K);
for (int j=;j<K;j++) {
scanf ("%d",&x);
Node[num].child.push_back(x);
}
}
dfs (,);
int maxLength=,u=-;
for (int i=;i<=maxDepth;i++) {
if (d[i]>maxLength) {
maxLength=d[i];
u=i;
}
}
printf ("%d %d",maxLength,u);
return ;
}

A1106 Lowest Price In Supply Train(25)

 #include <iostream>
#include <cmath>
#include <vector>
using namespace std;
vector<vector<int> > v;
vector<int> level;
int count=,minlevel=;
void dfs(int u,int l){
if(v[u].size()==){
level[u]=l;
if(minlevel>l){
count=;
minlevel=l;
}else if(minlevel==l){
count++;
}
}else{
for(int w=;w<v[u].size();w++){
dfs(v[u][w],l+);
}
}
}
int main(){
int n;
double price,rate;
cin>>n>>price>>rate;
v.resize(n),level.resize(n);
for(int i=;i<n;i++){
int cnt;
cin>>cnt;
v[i].resize(cnt);
for(int j=;j<cnt;j++){
cin>>v[i][j];
}
}
dfs(,);
double ans=price*pow(+rate/100.0,minlevel);
printf("%.4lf %d",ans,count);
return ;
}

A1127 ZigZagging on a Tree(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int post[maxn],in[maxn];
struct node {
int data;
node * left;
node * right;
};
node * create (int postL,int postR,int inL,int inR) {
if (postL>postR) return NULL;
node * root=new node;
root->data=post[postR];
int k;
for (k=inL;k<=inR;k++)
if (in[k]==post[postR]) break;
int numLeft=k-inL;
root->left=create (postL,postL+numLeft-,inL,k-);
root->right=create (postL+numLeft,postR-,k+,inR);
return root;
}
vector<int> d[maxn];
int maxdepth=-;
void dfs (node * root,int depth) {
if (root==NULL) return;
maxdepth=max(maxdepth,depth);
d[depth].push_back(root->data);
dfs (root->left,depth+);
dfs (root->right,depth+);
}
int N;
int main () {
scanf ("%d",&N);
for (int i=;i<N;i++)
scanf ("%d",&in[i]);
for (int i=;i<N;i++)
scanf ("%d",&post[i]);
node * root=create (,N-,,N-);
dfs (root,);
for (int i=;i<=maxdepth;i++) {
if (i%==) {
for (int j=d[i].size()-;j>=;j--) {
if (j!=d[i].size()-) printf (" ");
printf ("%d",d[i][j]);
}
if (i!=maxdepth) printf (" ");
}
else {
for (int j=;j<d[i].size();j++) {
if (j!=) printf (" ");
printf ("%d",d[i][j]);
}
if (i!=maxdepth) printf (" ");
}
}
return ;
}

专题三  二叉树的遍历

A1020 Tree Traversals(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
node * left=NULL;
node * right=NULL;
};
int post[maxn],in[maxn];
node * create (int postL,int postR,int inL,int inR) {
if (postL>postR) return NULL;
node * root=new node;
root->data=post[postR];
int k;
for (k=inL;k<=inR;k++)
if (in[k]==post[postR]) break;
int numLeft=k-inL;
root->left=create (postL,postL+numLeft-,inL,k-);
root->right=create (postL+numLeft,postR-,k+,inR);
return root;
}
void bfs (node * root) {
queue<node *> q;
q.push (root);
while (!q.empty()) {
node * now=q.front();
q.pop();
if (now!=root) printf (" ");
printf ("%d",now->data);
if (now->left) q.push(now->left);
if (now->right) q.push(now->right);
}
}
int main () {
int N;
scanf ("%d",&N);
for (int i=;i<N;i++)
scanf ("%d",&post[i]);
for (int i=;i<N;i++)
scanf ("%d",&in[i]);
node * root=create (,N-,,N-);
bfs (root);
return ;
}

A1043 Is It a BST(25)

#include<cstdio>
#include<vector>
using namespace std;
struct node {
int data;
node * lchild;
node * rchild;
};
void insert (node * &root,int data) {
if (root==NULL) {
root=new node;
root->data=data;
root->lchild=root->rchild=NULL;
return;
}
if (data<root->data)
insert (root->lchild,data);
else insert (root->rchild,data);
}
void preOrder (node * root,vector<int>& vi) {
if (root==NULL) return;
vi.push_back(root->data);
preOrder (root->lchild,vi);
preOrder (root->rchild,vi);
}
void preOrderMirror (node * root,vector<int>& vi) {
if (root==NULL) return;
vi.push_back(root->data);
preOrderMirror (root->rchild,vi);
preOrderMirror (root->lchild,vi);
}
void postOrder (node * root,vector<int>& vi) {
if (root==NULL) return;
postOrder (root->lchild,vi);
postOrder (root->rchild,vi);
vi.push_back(root->data);
}
void postOrderMirror (node * root,vector<int>& vi) {
if (root==NULL) return;
postOrderMirror (root->rchild,vi);
postOrderMirror (root->lchild,vi);
vi.push_back(root->data);
}
vector<int> origin,pre,preM,post,postM;
int main () {
int N,data;
scanf ("%d",&N);
node * root=NULL;
for (int i=;i<N;i++) {
scanf ("%d",&data);
origin.push_back(data);
insert (root,data);
}
preOrder (root,pre);
preOrderMirror (root,preM);
postOrder (root,post);
postOrderMirror (root,postM);
if (origin==pre) {
printf ("YES\n");
for (int i=;i<N;i++) {
printf ("%d",post[i]);
if (i<N-) printf (" ");
}
}
else if (origin==preM) {
printf ("YES\n");
for (int i=;i<N;i++) {
printf ("%d",postM[i]);
if (i<N-) printf (" ");
}
}
else printf ("NO");
return ;
}

A1064 CBT(30)

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int n,number[maxn],CBT[maxn],index=;
void inOrder (int root) {
if (root>n) return;
inOrder(root*);
CBT[root]=number[index++];
inOrder(root*+);
}
int main () {
scanf ("%d",&n);
for (int i=;i<n;i++) {
scanf ("%d",&number[i]);
}
sort (number,number+n);
inOrder();
for (int i=;i<=n;i++) {
printf ("%d",CBT[i]);
if (i<n) printf (" ");
}
return ;
}

A1086 Tree Traversals Again(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int pre[maxn],in[maxn];
int cnt1=,cnt2=;
stack<int> st;
struct node {
int data;
node * left;
node * right;
};
node * create (int preL,int preR,int inL,int inR) {
if (preL>preR) return NULL;
node * root=new node;
root->data=pre[preL];
int k;
for (k=inL;k<=inR;k++)
if (in[k]==pre[preL]) break;
int numLeft=k-inL;
root->left=create(preL+,preL+numLeft,inL,k-);
root->right=create(preL+numLeft+,preR,k+,inR);
return root;
}
int num=,N;
void postOrder (node * root) {
if (root==NULL) return;
postOrder (root->left);
postOrder (root->right);
printf ("%d",root->data);
num++;
if (num<N) printf (" ");
}
int main () {
scanf ("%d",&N);
string s;
int x;
for (int i=;i<N*;i++) {
cin>>s;
if (s=="Push") {
scanf ("%d",&x);
st.push(x);
pre[cnt1++]=x;
}
else {
in[cnt2++]=st.top();
st.pop();
}
}
node * root=create(,N-,,N-);
postOrder (root);
return ;
}

A1099 Build A BST(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
int left;
int right;
}Node[maxn];
int a[maxn];
int cnt=;
void inOrder (int root) {
if (root==-) return;
inOrder (Node[root].left);
Node[root].data=a[cnt++];
inOrder (Node[root].right);
}
int num=,N;
void bfs (int root) {
queue<int> q;
q.push(root);
while (!q.empty()) {
int now=q.front();
q.pop();
printf ("%d",Node[now].data);
num++;
if (num<N) printf (" ");
if (Node[now].left!=-) q.push(Node[now].left);
if (Node[now].right!=-) q.push(Node[now].right);
}
}
int main () {
scanf ("%d",&N);
int u,v;
for (int i=;i<N;i++) {
scanf ("%d %d",&u,&v);
Node[i].left=u;
Node[i].right=v;
}
for (int i=;i<N;i++)
scanf ("%d",&a[i]);
sort (a,a+N);
inOrder ();
bfs ();
return ;
}

A1102 Invert a Binary Tree(25)

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=;
struct node {
int left;
int right;
}Node[maxn];
int num,N;
bool isRoot[maxn]={false};
int strToint (char c) {
if (c=='-') return -;
else {
isRoot[c-'']=true;
return c-'';}
}
void postOrder (int root) {
if (root==-) return;
postOrder(Node[root].left);
postOrder(Node[root].right);
swap(Node[root].left,Node[root].right);
}
void BFS (int root) {
queue<int> q;
q.push(root);
while (!q.empty()) {
int now=q.front();
q.pop();
printf ("%d",now);
num++;
if (num<N) printf (" ");
if (Node[now].left!=-) q.push(Node[now].left);
if (Node[now].right!=-) q.push(Node[now].right);
}
}
void inOrder (int root) {
if (root==-) return;
inOrder(Node[root].left);
printf ("%d",root);
num++;
if (num<N) printf (" ");
inOrder(Node[root].right);
}
int main () {
scanf ("%d",&N);
char u,v;
int left,right;
for (int i=;i<N;i++) {
scanf ("%*c%c %c",&u,&v);
Node[i].left=strToint(u);
Node[i].right=strToint(v);
}
int root;
for (root=;root<N;root++)
if (isRoot[root]==false) break;
postOrder(root);
num=;
BFS(root);
printf ("\n");
num=;
inOrder(root);
return ;
}

A1110 CBT(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int left;
int right;
}Node[maxn];
int lastindex;
bool notRoot[maxn]={false};
bool bfs (int root) {
queue<int> q;
q.push(root);
int cnt=;
while (!q.empty()) {
int now=q.front();
q.pop();
if (cnt!=&&!(Node[now].left==-&&Node[now].right==-)) return false;
else if (Node[now].left==-&&Node[now].right!=-) return false;
else if ((Node[now].left!=-&&Node[now].right==-)||(Node[now].left==-&&Node[now].right==-)) {
cnt++;
}
if (Node[now].left!=-) q.push(Node[now].left);
if (Node[now].right!=-) q.push(Node[now].right);
if (q.empty()) lastindex=now;
}
return true;
}
int getnum (string s) {
if (s=="-") return -;
int ans=;
for (int i=;i<s.length();i++)
ans=ans*+s[i]-'';
return ans;
}
int main () {
string s1,s2;
int N;
scanf ("%d",&N);
for (int i=;i<N;i++) {
cin>>s1>>s2;
Node[i].left=getnum(s1);
Node[i].right=getnum(s2);
if (Node[i].left!=-) notRoot[Node[i].left]=true;
if (Node[i].right!=-) notRoot[Node[i].right]=true;
}
int root;
for (root=;root<N;root++)
if (notRoot[root]==false) break;
if (bfs(root)) printf ("YES %d",lastindex);
else printf ("NO %d",root);
return ;
}

A1130 Infix Expression(25)

#include<iostream>
#include<string>
using namespace std;
const int maxn=;
struct node {
string data;
int left,right;
}a[maxn];
string dfs (int root) {
if (a[root].left==-&&a[root].right==-)
return a[root].data;
if (a[root].left==-&&a[root].right!=-)
return "("+a[root].data+dfs(a[root].right)+")";
if (a[root].left!=-&&a[root].right!=-)
return "("+dfs(a[root].left)+a[root].data+dfs(a[root].right)+")";
}
int main () {
int isRoot[maxn]={},n,root=;
cin>>n;
for (int i=;i<=n;i++) {
cin>>a[i].data>>a[i].left>>a[i].right;
if (a[i].left!=-) isRoot[a[i].left]=;
if (a[i].right!=-) isRoot[a[i].right]=;
}
while (isRoot[root]==)
root++;
string ans=dfs (root);
if (ans[]=='(') ans=ans.substr(,ans.size()-);
cout<<ans;
return ;
}

A1135 Is it A Red-black Tree(30)

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
vector<int> arr;
struct node {
int data;
node * left=NULL;
node * right=NULL;
};
node * insert (node * &root,int v) {
if (root==NULL) {
root=new node;
root->data=v;
}
else if (abs(v)<=abs(root->data))
root->left=insert (root->left,v);
else root->right=insert (root->right,v);
return root;
}
bool judge1 (node * root) {
if (root==NULL) return true;
if (root->data<) {
if (root->left!=NULL&&root->left->data<) return false;
if (root->right!=NULL&&root->right->data<) return false;
}
return judge1(root->left)&&judge1(root->right);
}
int getNum (node * root) {
if (root==NULL) return ;
int left=getNum(root->left);
int right=getNum(root->right);
return root->data>?max(left,right)+:max(left,right);
}
bool judge2 (node * root) {
if (root==NULL) return true;
int left=getNum(root->left);
int right=getNum(root->right);
if (left!=right) return false;
return judge2(root->left)&&judge2(root->right);
}
int main () {
int k,n;
scanf ("%d",&k);
for (int i=;i<k;i++) {
scanf ("%d",&n);
arr.resize(n);
node * root=NULL;
for (int j=;j<n;j++) {
scanf ("%d",&arr[j]);
root=insert (root,arr[j]);
}
if (arr[]<||judge1(root)==false||judge2(root)==false)
printf ("No\n");
else printf ("Yes\n");
}
return ;
}

A1143 LCA(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn],M,N;
unordered_map<int,int> pos;
int main () {
scanf ("%d %d",&M,&N);
for (int i=;i<N;i++) {
scanf ("%d",&a[i]);
pos[a[i]]=;
}
int u,v;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
if (pos[u]==&&pos[v]==) printf ("ERROR: %d and %d are not found.\n",u,v);
else if (pos[u]==||pos[v]==) printf ("ERROR: %d is not found.\n",pos[u]==?u:v);
else {
int j;
for (j=;j<N;j++)
if ((a[j]>=u&&a[j]<=v)||(a[j]<=u&&a[j]>=v)) break;
if ((a[j]>u&&a[j]<v)||(a[j]>v&&a[j]<u)) printf ("LCA of %d and %d is %d.\n",u,v,a[j]);
else if (a[j]==u) printf ("%d is an ancestor of %d.\n",u,v);
else if (a[j]==v) printf ("%d is an ancestor of %d.\n",v,u);
}
}
return ;
}

A1147 Heaps(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn],N,T,num=;
void postOrder (int root) {
if (root>N) return;
postOrder (root*);
postOrder (root*+);
printf ("%d",a[root]);
num++;
if (num<N) printf (" ");
}
int main () {
scanf ("%d %d",&T,&N);
while (T--) {
num=;
for (int i=;i<=N;i++)
scanf ("%d",&a[i]);
int isMax=,isMin=;
for (int i=;i<=N;i++) {
if (a[i/]>a[i]) isMin=;
if (a[i/]<a[i]) isMax=;
}
if (isMax==) printf ("Max Heap\n");
else if (isMin==) printf ("Min Heap\n");
else printf ("Not Heap\n");
postOrder ();
printf ("\n");
}
return ;
}

A1151 LCA in a Binary Tree(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
node * left;
node * right;
};
int M,N;
int pre[maxn],in[maxn];
unordered_map<int,int> pos;
int father[maxn*];
int depth[maxn*];
node * create (int preL,int preR,int inL,int inR) {
if (preL>preR) return NULL;
node * root=new node;
root->data=pre[preL];
int k;
for (k=inL;k<=inR;k++)
if (in[k]==pre[preL]) break;
int numLeft=k-inL;
root->left=create(preL+,preL+numLeft,inL,k-);
if (root->left!=NULL) father[root->left->data]=root->data;
root->right=create(preL+numLeft+,preR,k+,inR);
if (root->right!=NULL) father[root->right->data]=root->data;
return root;
}
void bfs (node * root) {
queue<node *> q;
q.push(root);
depth[root->data]=;
while (!q.empty()) {
node * now=q.front();
q.pop();
if (now->left) {q.push(now->left);depth[now->left->data]=depth[now->data]+;}
if (now->right) {q.push(now->right);depth[now->right->data]=depth[now->data]+;}
}
}
void lca (int u,int v) {
int tu=u;
int tv=v;
while (depth[tu]<depth[tv]) {
tv=father[tv];
}
while (depth[tu]>depth[tv]) {
tu=father[tu];
}
while (tu!=tv) {
tu=father[tu];
tv=father[tv];
}
if (tu==u) printf ("%d is an ancestor of %d.\n",u,v);
else if (tu==v) printf ("%d is an ancestor of %d.\n",v,u);
else printf ("LCA of %d and %d is %d.\n",u,v,tu);
}
int main () {
scanf ("%d %d",&M,&N);
for (int i=;i<=N;i++) father[i]=i;
for (int i=;i<=N;i++) {
scanf ("%d",&in[i]);
pos[in[i]]=i;
}
for (int i=;i<=N;i++)
scanf ("%d",&pre[i]);
node * root=create(,N,,N);
bfs(root);
int u,v;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
if (pos[u]==&&pos[v]==) printf ("ERROR: %d and %d are not found.\n",u,v);
else if (pos[u]==||pos[v]==) printf ("ERROR: %d is not found.\n",pos[u]==?u:v);
else lca (u,v);
}
return ;
}

A1155 Heap Paths(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn],N;
vector<int> path;
void dfs (int root) {
if (root>N) return;
path.push_back(root);
if (root*>N) {
for (int i=;i<path.size();i++) {
if (i!=) printf (" ");
printf ("%d",a[path[i]]);
}
printf ("\n");
path.pop_back();
return;
}
dfs (root*+);
dfs (root*);
path.pop_back();
}
int main () {
scanf ("%d",&N);
for (int i=;i<=N;i++)
scanf ("%d",&a[i]);
dfs ();
int isMax=,isMin=;
for (int i=;i<=N;i++) {
if (a[i/]>a[i]) isMin=;
if (a[i/]<a[i]) isMax=;
}
if (isMin==) printf ("Min Heap");
else if (isMax==) printf ("Max Heap");
else printf ("Not Heap");
return ;
}

专题四  深度优先搜索(DFS)

A1013 Battle Over CIties(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int> g[maxn];
bool visit[maxn]={false};
int occupy,N,M,K;
void dfs (int s) {
if (s==occupy) return;
visit[s]=true;
for (int i=;i<g[s].size();i++)
if (visit[g[s][i]]==false&&g[s][i]!=occupy)
dfs (g[s][i]);
}
int dfsTrave () {
int block=;
fill (visit,visit+maxn,false);
for (int i=;i<=N;i++) {
if (visit[i]==false&&i!=occupy) {
dfs (i);
block++;
}
}
return block-;
}
int main () {
scanf ("%d %d %d",&N,&M,&K);
int u,v;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for (int i=;i<K;i++) {
scanf ("%d",&occupy);
printf ("%d\n",dfsTrave());
}
return ;
}

A1021 Deepest Root(25)

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
vector<int> g[maxn];
bool visit[maxn]={false};
int N;
void dfs (int v) {
visit[v]=true;
for (int i=;i<g[v].size();i++)
if (visit[g[v][i]]==false)
dfs (g[v][i]);
}
int block=;
void dfstrave () {
for (int i=;i<=N;i++)
if (visit[i]==false) {
dfs (i);
block++;
}
}
int maxheight=,pre=-;
vector<int> v1,v2;
void dfsfind (int s,int index,int pre) {
if (index>maxheight) {
v1.clear();
v1.push_back(s);
maxheight=index;
}
else if (index==maxheight)
v1.push_back(s);
//visit[s]=true;
for (int i=;i<g[s].size();i++) {
if (g[s][i]==pre) continue;
dfsfind (g[s][i],index+,s);
}
}
void dfsfind2 (int s,int index,int pre) {
if (index>maxheight) {
v2.clear();
v2.push_back(s);
maxheight=index;
}
else if (index==maxheight)
v2.push_back(s);
//visit[s]=true;
for (int i=;i<g[s].size();i++) {
if (g[s][i]==pre) continue;
dfsfind2 (g[s][i],index+,s);
}
}
int main () {
scanf ("%d",&N);
int u,v;
for (int i=;i<N;i++) {
scanf ("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfstrave();
if (block!=) {
printf ("Error: %d components",block);
return ;
}
dfsfind(,,-);
dfsfind2(v1[],,-);
for (int i=;i<v2.size();i++)
v1.push_back(v2[i]);
int course[maxn]={};
sort (v1.begin(),v1.end());
for (int i=;i<v1.size();i++) {
if (course[v1[i]]==) {
printf ("%d\n",v1[i]);
course[v1[i]]=;
}
}
return ;
}

A1103 Integer Factoriztion(30)

#include<bits/stdc++.h>
using namespace std;
int N,K,P;
vector<int> v,tmp,path;
void init () {
int tmp=;
int index=;
while (tmp<=N) {
v.push_back(tmp);
tmp=pow(index,P);
index++;
}
}
int maxFacSum=-;
void dfs (int nowindex,int nowK,int nowSum,int facSum) {
if (nowK>K||nowSum>N) return;
if (nowK==K) {
if (nowSum==N) {
if (facSum>maxFacSum) {
path=tmp;
maxFacSum=facSum;
}
}
return;
}
while (nowindex>=) {
tmp.push_back(nowindex);
dfs (nowindex,nowK+,nowSum+v[nowindex],facSum+nowindex);
tmp.pop_back();
nowindex--;
//if (nowindex==1) return;
}
}
int main () {
scanf ("%d %d %d",&N,&K,&P);
init ();
dfs (v.size()-,,,);
if (maxFacSum==-) {
printf ("Impossible");
return ;
}
printf ("%d = ",N);
for (int i=;i<path.size();i++) {
if (i!=) printf (" + ");
printf ("%d^%d",path[i],P);
}
return ;
}

A1131 Subway Map(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
unordered_map<int,int> line;
vector<int> g[maxn];
vector<int> path,tmp;
bool visit[maxn]={false};
int N,st,ed;
int transfer (vector<int> v) {
int cnt=;
for (int i=;i<v.size()-;i++)
if (line[v[i-]*+v[i]]!=line[v[i]*+v[i+]]) cnt++;
return cnt;
}
int minCnt=1e9,minTransferCnt=1e9;
void dfs (int s) {
tmp.push_back(s);
visit[s]=true;
if (s==ed) {
int transferCnt=transfer(tmp);
if (tmp.size()<minCnt) {
minCnt=tmp.size();
minTransferCnt=transferCnt;
path=tmp;
}
else if (tmp.size()==minCnt&&transferCnt<minTransferCnt) {
minTransferCnt=transferCnt;
path=tmp;
}
tmp.pop_back();
visit[s]=false;
return;
}
for (int i=;i<g[s].size();i++)
if (visit[g[s][i]]==false) dfs (g[s][i]);
tmp.pop_back();
visit[s]=false;
}
int main () {
scanf ("%d",&N);
int k,u,v;
for (int i=;i<=N;i++) {
scanf ("%d %d",&k,&u);
for (int j=;j<k;j++) {
scanf ("%d",&v);
g[u].push_back(v);
g[v].push_back(u);
line[u*+v]=line[v*+u]=i;
u=v;
}
}
int q;
scanf ("%d",&q);
for (int i=;i<q;i++) {
fill (visit,visit+maxn,false);
minCnt=1e9,minTransferCnt=1e9;
scanf ("%d %d",&st,&ed);
dfs (st);
printf ("%d\n",path.size()-);
int preLine=line[path[]*+path[]],pre=path[];
for (int j=;j<path.size();j++) {
if (line[path[j-]*+path[j]]!=preLine) {
printf ("Take Line#%d from %04d to %04d.\n",preLine,pre,path[j-]);
pre=path[j-];
preLine=line[path[j-]*+path[j]];
}
}
printf ("Take Line#%d from %04d to %04d.\n",preLine,pre,ed);
}
return ;
}

A1148 Werewolf(20)

#include<stdio.h>
#include<math.h>
int ans[];
int judge[];
int N;
int main () {
scanf ("%d",&N);
for (int i=;i<=N;i++)
scanf ("%d",&judge[i]);
int lie,cnt;
for (int i=;i<N;i++) {
for (int j=i+;j<=N;j++) {
lie=;cnt=;
for (int w=;w<=N;w++)
ans[w]=;
ans[i]=-;ans[j]=-;
for (int w=;w<=N;w++)
if (judge[w]*ans[abs(judge[w])]<) {
lie++;
if (w==i||w==j) cnt++;
}
if (lie==&&cnt==) {
printf ("%d %d",i,j);
return ;
}
}
}
printf ("No Solution");
return ;
}

专题五  广度优先搜索(BFS)

A1076 Forwards on Weibo(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int> g[maxn];
struct node {
int id,layer;
};
int N,L;
int bfs (int s) {
int cnt=;
bool visit[maxn]={false};
visit[s]=true;
queue<node> q;
q.push({s,});
while (!q.empty()) {
node now=q.front();
q.pop();
int id=now.id;
int layer=now.layer;
if (layer<L) {
for (int i=;i<g[id].size();i++) {
if (visit[g[id][i]]==false) {
q.push({g[id][i],layer+});
cnt++;
visit[g[id][i]]=true;
}
}
}
}
return cnt;
}
int main () {
scanf ("%d %d",&N,&L);
int k,x;
for (int i=;i<=N;i++) {
scanf ("%d",&k);
for (int j=;j<k;j++) {
scanf ("%d",&x);
g[x].push_back(i);
}
}
int q;
scanf ("%d",&q);
for (int i=;i<q;i++) {
scanf ("%d",&x);
printf ("%d\n",bfs(x));
}
return ;
}

A1091 Acute Stroke(30)

#include<bits/stdc++.h>
using namespace std;
int n,m,l,t;
bool visit[][][]={false};
int adj[][][];
int X[]={,,,-,,};
int Y[]={,,,,-,};
int Z[]={,,,,,-};
struct node {
int x,y,z;
};
int judge (int x,int y,int z) {
if (x>=m||x<||y>=n||y<||z>=l||z<)
return ;
if (visit[x][y][z]==true||adj[x][y][z]==) return ;
return ;
}
int bfs (int x,int y,int z) {
queue<node> q;
int cnt=;
q.push({x,y,z});
visit[x][y][z]=true;
while (!q.empty()) {
node now=q.front();
q.pop();
for (int i=;i<;i++) {
int tx=now.x+X[i];
int ty=now.y+Y[i];
int tz=now.z+Z[i];
if (judge(tx,ty,tz)) {
q.push({tx,ty,tz});
visit[tx][ty][tz]=true;
cnt++;
}
}
}
if (cnt<t) return ;
return cnt;
}
int main () {
scanf ("%d %d %d %d",&m,&n,&l,&t);
for (int i=;i<l;i++)
for (int j=;j<m;j++)
for (int k=;k<n;k++)
scanf ("%d",&adj[j][k][i]);
int cnt=;
for (int i=;i<l;i++)
for (int j=;j<m;j++)
for (int k=;k<n;k++)
if (visit[j][k][i]==false&&adj[j][k][i]==) {
cnt+=bfs(j,k,i);
}
printf ("%d\n",cnt);
return ;
}

专题六  最短路径

A1003 Emergency(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
int g[maxn][maxn],d[maxn],w[maxn],num[maxn],weight[maxn];
bool visit[maxn];
int N,M,st,ed;
void init () {
for (int i=;i<N;i++)
for (int j=;j<N;j++)
g[i][j]=inf;
}
void dijkstra (int s) {
fill (d,d+maxn,inf);
fill (w,w+maxn,);
fill (num,num+maxn,);
fill (visit,visit+maxn,false);
d[s]=;
w[s]=weight[s];
num[s]=;
for (int i=;i<N;i++) {
int u=-,min=inf;
for (int j=;j<N;j++)
if (visit[j]==false&&d[j]<min) {
u=j;
min=d[j];
}
if (u==-) return;
visit[u]=true;
for (int v=;v<N;v++) {
if (visit[v]==false&&g[u][v]!=inf) {
if (d[u]+g[u][v]<d[v]) {
d[v]=d[u]+g[u][v];
w[v]=w[u]+weight[v];
num[v]=num[u];
}
else if (d[u]+g[u][v]==d[v]) {
num[v]+=num[u];
if (w[u]+weight[v]>w[v])
w[v]=w[u]+weight[v];
}
}
}
}
}
int main () {
scanf ("%d %d %d %d",&N,&M,&st,&ed);
for (int i=;i<N;i++)
scanf ("%d",&weight[i]);
init ();
int u,v;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
scanf ("%d",&g[u][v]);
g[v][u]=g[u][v];
}
dijkstra (st);
printf ("%d %d",num[ed],w[ed]);
return ;
}

A1018 Public Bike Management(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
int g[maxn][maxn],d[maxn],weight[maxn];
bool visit[maxn]={false};
vector<int> pre[maxn];
vector<int> path,tmp;
int Cmax,N,Sp,M;
void init () {
for (int i=;i<=N;i++)
for (int j=;j<=N;j++)
g[i][j]=inf;
}
void dijkstra (int s) {
fill (d,d+maxn,inf);
fill (visit,visit+maxn,false);
d[s]=;
for (int i=;i<=N;i++) {
int u=-,min=inf;
for (int j=;j<=N;j++)
if (visit[j]==false&&d[j]<min) {
u=j;
min=d[j];
}
if (u==-) return;
visit[u]=true;
for (int v=;v<=N;v++) {
if (visit[v]==false&&g[u][v]!=inf) {
if (d[u]+g[u][v]<d[v]) {
d[v]=d[u]+g[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if (d[u]+g[u][v]==d[v])
pre[v].push_back(u);
}
}
}
}
int minNeed,minBack;
void dfs (int v) {
tmp.push_back(v);
if (v==) {
int need=,back=;
for (int i=tmp.size()-;i>=;i--) {
int id=tmp[i];
if (weight[id]>) back+=weight[id];
else {
if (back>(-weight[id])) back+=weight[id];
else {
need+=(-weight[id])-back;
back=;
}
}
}
if (need<minNeed) {
minNeed=need;
minBack=back;
path=tmp;
}
else if (need==minNeed&&back<minBack) {
minBack=back;
path=tmp;
}
tmp.pop_back();
return;
}
for (int i=;i<pre[v].size();i++)
dfs (pre[v][i]);
tmp.pop_back();
}
int main () {
scanf ("%d %d %d %d",&Cmax,&N,&Sp,&M);
for (int i=;i<=N;i++) {
scanf ("%d",&weight[i]);
weight[i]-=Cmax/;
}
int u,v;
init ();
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
scanf ("%d",&g[u][v]);
g[v][u]=g[u][v];
}
dijkstra ();
minNeed=1e9;
minBack=1e9;
dfs (Sp);
printf ("%d ",minNeed);
for (int i=path.size()-;i>=;i--) {
if (i!=path.size()-) printf ("->");
printf ("%d",path[i]);
}
printf (" %d",minBack);
return ;
}

A1030 Travel Plan(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
int g[maxn][maxn],cost[maxn][maxn],d[maxn],c[maxn];
int pre[maxn];
bool visit[maxn];
int N,M,st,ed;
void init () {
for (int i=;i<N;i++)
for (int j=;j<N;j++)
g[i][j]=inf;
}
void dijkstra (int s) {
fill (d,d+maxn,inf);
fill (c,c+maxn,inf);
d[s]=;
c[s]=;
for (int i=;i<N;i++) {
int u=-,min=inf;
for (int j=;j<N;j++)
if (visit[j]==false&&d[j]<min) {
u=j;
min=d[j];
}
if (u==-) return;
visit[u]=true;
for (int v=;v<N;v++) {
if (visit[v]==false&&g[u][v]!=inf) {
if (d[u]+g[u][v]<d[v]) {
d[v]=d[u]+g[u][v];
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
else if (d[u]+g[u][v]==d[v]&&c[u]+cost[u][v]<c[v]) {
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
}
}
}
}
void dfs (int v) {
if (v==st) {
printf ("%d",v);
return;
}
dfs (pre[v]);
printf (" %d",v);
}
int main () {
scanf ("%d %d %d %d",&N,&M,&st,&ed);
init ();
int u,v;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
scanf ("%d %d",&g[u][v],&cost[u][v]);
g[v][u]=g[u][v];
cost[v][u]=cost[u][v];
}
dijkstra (st);
dfs (ed);
printf (" %d %d",d[ed],c[ed]);
return ;
}

A1072 Gas Station(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
int d[maxn][maxn];
int N,M,K,D;
void init () {
for (int i=;i<=N+M;i++)
for (int j=;j<=N+M;j++)
d[i][j]=inf;
}
void floyd () {
for (int k=;k<=N+M;k++)
for (int i=N+;i<=N+M;i++)
for (int j=;j<=i;j++) {
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
d[j][i]=d[i][j];
}
}
int getnum (string s) {
int flag=;
if (s[]=='G') flag++;
int ans=;
for (int i=flag;i<s.length();i++)
ans=ans*+s[i]-'';
if (flag==) return ans;
else return ans+N;
}
int main () {
scanf ("%d %d %d %d",&N,&M,&K,&D);
string s1,s2;
int u,v;
init ();
for (int i=;i<K;i++) {
cin>>s1>>s2;
u=getnum(s1);
v=getnum(s2);
scanf ("%d",&d[u][v]);
d[v][u]=d[u][v];
}
floyd ();
int maxDistance=-;double minAvg=inf;
for (int i=N+;i<=N+M;i++) {
int flag=,minDistance=inf;double avg=;
for (int j=;j<=N;j++) {
if (d[i][j]>D) flag++;
if (d[i][j]<minDistance) {
minDistance=d[i][j];
}
avg+=d[i][j];
}
if (flag!=) continue;
avg/=N;
if (minDistance>maxDistance) {
maxDistance=minDistance;
minAvg=avg;
u=i;
}
else if (minDistance==maxDistance&&avg<minAvg) {
minAvg=avg;
u=i;
}
}
if (maxDistance==-) printf ("No Solution");
else printf ("G%d\n%.1f %.1f",u-N,(double)maxDistance,minAvg);
return ;
}

A1087 All Roads Lead to Rome(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
unordered_map<string,int> pos1;
unordered_map<int,string> pos2;
int g[maxn][maxn],d[maxn],w[maxn],weight[maxn],pre[maxn],num[maxn],numNode[maxn];
bool visit[maxn]={false};
int N,M;
void init () {
for (int i=;i<=N;i++)
for (int j=;j<=N;j++)
g[i][j]=inf;
}
void dijkstra (int s) {
fill (d,d+maxn,inf);
fill (visit,visit+maxn,false);
fill (w,w+maxn,);
fill (num,num+maxn,);
fill (numNode,numNode+maxn,inf);
num[s]=;
d[s]=;
w[s]=weight[s];
numNode[s]=;
for (int i=;i<=N;i++) {
int u=-,min=inf;
for (int j=;j<=N;j++)
if (visit[j]==false&&d[j]<min) {
u=j;
min=d[j];
}
if (u==-) return;
visit[u]=true;
for (int v=;v<=N;v++) {
if (visit[v]==false&&g[u][v]!=inf) {
if (d[u]+g[u][v]<d[v]) {
d[v]=d[u]+g[u][v];
pre[v]=u;
w[v]=w[u]+weight[v];
num[v]=num[u];
numNode[v]=numNode[u]+;
}
else if (d[u]+g[u][v]==d[v]) {
num[v]+=num[u];
if (w[u]+weight[v]>w[v]) {
w[v]=w[u]+weight[v];
pre[v]=u;
numNode[v]=numNode[u]+;
}
else if (w[u]+weight[v]==w[v]&&numNode[u]+<numNode[v]) {
numNode[v]=numNode[u]+;
pre[v]=u;
}
}
}
}
}
}
void dfs (int v) {
if (v==) {
cout<<pos2[v];
return;
}
dfs (pre[v]);
cout<<"->"<<pos2[v];
}
int main () {
scanf ("%d %d",&N,&M);
string s1,s2;
int x;
cin>>s1;
pos1[s1]=;
pos2[]=s1;
for (int i=;i<=N;i++) {
cin>>s1>>x;
pos1[s1]=i;
pos2[i]=s1;
weight[i]=x;
}
init ();
for (int i=;i<M;i++) {
cin>>s1>>s2>>x;
g[pos1[s1]][pos1[s2]]=g[pos1[s2]][pos1[s1]]=x;
}
dijkstra ();
int ed=pos1["ROM"];
printf ("%d %d %d %d\n",num[ed],d[ed],w[ed],w[ed]/numNode[ed]);
dfs (ed);
return ;
}

A1111 Online Map(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
int g[maxn][maxn],cost[maxn][maxn],d[maxn],c[maxn],numNode[maxn];
bool visit[maxn]={false};
int pre[maxn];
vector<int> pre1,pre2;
int N,M,st,ed;
void init () {
for (int i=;i<N;i++)
for (int j=;j<N;j++) {
g[i][j]=inf;
cost[i][j]=inf;
}
}
void dijkstra1 (int s) {
fill (d,d+maxn,inf);
fill (c,c+maxn,inf);
fill (visit,visit+maxn,false);
d[s]=;
c[s]=;
for (int i=;i<N;i++) {
int u=-,min=inf;
for (int j=;j<N;j++)
if (visit[j]==false&&d[j]<min) {
u=j;
min=d[j];
}
if (u==-) return;
visit[u]=true;
for (int v=;v<N;v++) {
if (visit[v]==false&&g[u][v]!=inf) {
if (d[u]+g[u][v]<d[v]) {
d[v]=d[u]+g[u][v];
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
else if (d[u]+g[u][v]==d[v]&&c[u]+cost[u][v]<c[v]) {
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
}
}
}
}
void dfs1 (int v) {
if (v==st) {
pre1.push_back(v);
return;
}
dfs1 (pre[v]);
pre1.push_back(v);
}
void dijkstra2 (int s) {
fill (c,c+maxn,inf);
fill (numNode,numNode+maxn,inf);
fill (visit,visit+maxn,false);
c[s]=;
numNode[s]=;
for (int i=;i<N;i++) {
int u=-,min=inf;
for (int j=;j<N;j++)
if (visit[j]==false&&c[j]<min) {
u=j;
min=c[j];
}
if (u==-) return;
visit[u]=true;
for (int v=;v<N;v++) {
if (visit[v]==false&&cost[u][v]!=inf) {
if (c[u]+cost[u][v]<c[v]) {
c[v]=c[u]+cost[u][v];
numNode[v]=numNode[u]+;
pre[v]=u;
}
else if (c[u]+cost[u][v]==c[v]&&numNode[u]+<numNode[v]) {
numNode[v]=numNode[u]+;
pre[v]=u;
}
}
}
}
}
void dfs2 (int v) {
if (v==st) {
pre2.push_back(v);
return;
}
dfs2 (pre[v]);
pre2.push_back(v);
}
int main () {
scanf ("%d %d",&N,&M);
int u,v,flag;
init ();
for (int i=;i<M;i++) {
scanf ("%d %d %d",&u,&v,&flag);
scanf ("%d %d",&g[u][v],&cost[u][v]);
if (flag==) {
g[v][u]=g[u][v];
cost[v][u]=cost[u][v];
}
}
scanf ("%d %d",&st,&ed);
dijkstra1 (st);
dfs1 (ed);
int d1=d[ed];
dijkstra2 (st);
dfs2 (ed);
int t1=c[ed];
if (pre1==pre2) {
printf ("Distance = %d; Time = %d: ",d1,t1);
for (int i=;i<pre1.size();i++) {
if (i!=) printf (" -> ");
printf ("%d",pre1[i]);
}
return ;
}
printf ("Distance = %d: ",d1);
for (int i=;i<pre1.size();i++) {
if (i!=) printf (" -> ");
printf ("%d",pre1[i]);
}
printf ("\n");
printf ("Time = %d: ",t1);
for (int i=;i<pre2.size();i++) {
if (i!=) printf (" -> ");
printf ("%d",pre2[i]);
}
return ;
}

专题七  平衡二叉树

A1066 Root of AVL Tree(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
node * left=NULL;
node * right=NULL;
int height;
};
int getHeight (node * root) {
if (root==NULL) return ;
return root->height;
}
int getBalance (node * root) {
return getHeight(root->left)-getHeight(root->right);
}
void update (node * &root) {
root->height=max(getHeight(root->left),getHeight(root->right))+;
}
void L (node * &root) {
node * t=root->right;
root->right=t->left;
t->left=root;
update (root);
update (t);
root=t;
}
void R (node * &root) {
node * t=root->left;
root->left=t->right;
t->right=root;
update (root);
update (t);
root=t;
}
void insert (node * &root,int x) {
if (root==NULL) {
root=new node;
root->data=x;
return;
}
if (x<=root->data) {
insert (root->left,x);
update (root);
update (root->left);
if (getBalance(root)==) {
if (getBalance(root->left)==)
R (root);
else if (getBalance(root->left)==-) {
L (root->left);
R (root);
}
}
}
else {
insert (root->right,x);
update (root);
update (root->right);
if (getBalance(root)==-) {
if (getBalance(root->right)==-)
L (root);
else if (getBalance(root->right)==) {
R (root->right);
L (root);
}
}
}
}
int main () {
int N;
node * root=NULL;
scanf ("%d",&N);
int x;
for (int i=;i<N;i++) {
scanf ("%d",&x);
insert (root,x);
}
printf ("%d",root->data);
return ;
}

A1123 Is It A complete AVL Tree(30)

#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
struct node {
int v;
int height;
node * left;
node * right;
};
node * newNode (int v) {
node * Node=new node;
Node->v=v;
Node->height=;
Node->left=NULL;
Node->right=NULL;
return Node;
}
int getHeight (node * root) {
if (root==NULL) return ;
return root->height;
}
void update (node * &root) {
root->height=max(getHeight(root->left),getHeight(root->right))+;
}
int getBalance (node * root) {
return getHeight(root->left)-getHeight(root->right);
}
void L (node * &root) {
node * temp=root->right;
root->right=temp->left;
temp->left=root;
update(root);
update(temp);
root=temp;
}
void R (node * &root) {
node * temp=root->left;
root->left=temp->right;
temp->right=root;
update(root);
update(temp);
root=temp;
}
void insert (node * &root,int x) {
if (root==NULL) {
root=newNode(x);
return;
}
if (x<root->v) {
insert (root->left,x);
update(root);
if (getBalance(root)==) {
if (getBalance(root->left)==)
R(root);
else if (getBalance(root->left)==-) {
L(root->left);
R(root);
}
}
}
else {
insert (root->right,x);
update(root);
if (getBalance(root)==-) {
if (getBalance(root->right)==-)
L(root);
else if (getBalance(root->right)==) {
R(root->right);
L(root);
}
}
}
}
int num=,N;
vector<int> vi;
int flag=,cnt=;
void BFS (node * root) {
queue<node *> q;
q.push(root);
while (!q.empty()) {
node * now=q.front();
q.pop();
vi.push_back(now->v);
if (!(!now->left&&!now->right)&&cnt==)
flag=;
if (!now->left&&now->right)
flag=;
if ((now->left&&!now->right)||(!now->left&&!now->right))
cnt++;
if (now->left) q.push(now->left);
if (now->right) q.push(now->right);
}
}
int main () {
int N;
scanf ("%d",&N);
int x;
node * root=NULL;
for (int i=;i<N;i++) {
scanf ("%d",&x);
insert (root,x);
}
BFS (root);
for (int i=;i<vi.size();i++) {
printf ("%d",vi[i]);
if (i<vi.size()-) printf (" ");
}
printf ("\n");
if (flag==) printf ("NO");
else printf ("YES");
return ;
}

专题八  堆排序

A1089 Insert or Merge(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn],b[maxn];
int i,j,n;
void merge () {
int k=;
while (k<n) {
int flag=;
for (int i=;i<n;i++)
if (a[i]!=b[i]) flag=;
k*=;
for (int i=;i<n/k;i++) {
sort (a+i*k,a+(i+)*k);
}
sort (a+n/k*k,a+n);
if (flag==) break;
}
}
int main () {
scanf ("%d",&n);
for (i=;i<n;i++)
scanf ("%d",&a[i]);
for (i=;i<n;i++)
scanf ("%d",&b[i]);
for (i=;i<n-&&b[i]<=b[i+];i++);
for (j=i+;j<n&&a[j]==b[j];j++);
if (j==n) {
printf ("Insertion Sort\n");
sort (a,a+i+);
}
else {
printf ("Merge Sort\n");
merge ();
}
for (i=;i<n;i++) {
if (i!=) printf (" ");
printf ("%d",a[i]);
}
return ;
}

A1098 Insertioin or Heap sort(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn],b[maxn],n;
void downAdjust (int low,int high) {
int i=low,j=i*;
while (j<=high) {
if (j+<=high&&a[j+]>a[j])
j=j+;
if (a[j]>a[i]) {
swap (a[j],a[i]);
i=j;
j=i*;
}
else break;
}
}
void createHeap () {
for (int i=n/;i>=;i--)
downAdjust (i,n);
}
void heapSort () {
createHeap ();
int flag=;
for (int i=n;i>&&flag;i--) {
flag=;
for (int j=;j<=n;j++)
if (a[j]!=b[j]) flag=;
swap (a[i],a[]);
downAdjust (,i-);
}
}
int main () {
scanf ("%d",&n);
int i,j;
for (i=;i<=n;i++)
scanf ("%d",&a[i]);
for (i=;i<=n;i++)
scanf ("%d",&b[i]);
for (i=;i<n&&b[i]<=b[i+];i++);
for (j=i+;j<=n&&a[j]==b[j];j++);
if (j==n+) {
printf ("Insertion Sort\n");
sort (a+,a+i+);
}
else {
printf ("Heap Sort\n");
heapSort ();
}
for (i=;i<=n;i++) {
if (i!=) printf (" ");
printf ("%d",a[i]);
}
return ;
}

专题九  简单图论

A1134 Vertex Cover(25)

#include<iostream>
#include<vector>
using namespace std;
int main () {
int n,m,k,nv,a,b,num;
scanf ("%d %d",&n,&m);
vector<int> v[n];
for (int i=;i<m;i++) {
scanf ("%d%d",&a,&b);
v[a].push_back(i);
v[b].push_back(i);
}
scanf ("%d",&k);
for (int i=;i<k;i++) {
scanf ("%d",&nv);
int flag=;
vector<int> hash(m,);
for (int j=;j<nv;j++) {
scanf ("%d",&num);
for (int t=;t<v[num].size();t++)
hash[v[num][t]]=;
}
for (int j=;j<m;j++)
if (hash[j]==) {
printf ("No\n");
flag=;
break;
}
if (flag==) printf ("Yes\n");
}
return ;
}

A1139 First Contact(30)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
unordered_map<int,bool> arr;
struct node {
int a,b;
};
bool cmp (node x,node y) {
return x.a!=y.a?x.a<y.a:x.b<y.b;
}
int main () {
int n,m,k;
scanf ("%d %d",&n,&m);
vector<int> v[];
for (int i=;i<m;i++) {
string a,b;
cin>>a>>b;
if (a.length()==b.length()) {
v[abs(stoi(a))].push_back(abs(stoi(b)));
v[abs(stoi(b))].push_back(abs(stoi(a)));
}
arr[abs(stoi(a))*+abs(stoi(b))]=
arr[abs(stoi(b))*+abs(stoi(a))]=true;
}
scanf ("%d",&k);
for (int i=;i<k;i++) {
int c,d;
cin>>c>>d;
vector<node> ans;
for (int j=;j<v[abs(c)].size();j++) {
for (int k=;k<v[abs(d)].size();k++) {
if (v[abs(c)][j]==abs(d)||v[abs(d)][k]==abs(c))
continue;
if (arr[v[abs(c)][j]*+v[abs(d)][k]]==true)
ans.push_back(node{v[abs(c)][j],v[abs(d)][k]});
}
}
sort (ans.begin(),ans.end(),cmp);
printf ("%d\n",(int)ans.size());
for (int j=;j<ans.size();j++)
printf ("%04d %04d\n",ans[j].a,ans[j].b);
}
return ;
}

A1142 Maximal Clique(25)

#include<cstdio>
#include<vector>
using namespace std;
const int maxn=;
int g[maxn][maxn]={};
int main () {
int nv,ne,m,ta,tb,k;
scanf ("%d %d",&nv,&ne);
for (int i=;i<ne;i++) {
scanf ("%d %d",&ta,&tb);
g[ta][tb]=g[tb][ta]=;
}
scanf ("%d",&m);
for (int i=;i<m;i++) {
scanf ("%d",&k);
vector<int> v(k);
int hash[maxn]={};
int isclique=;
int isMaximal=;
for (int j=;j<k;j++) {
scanf ("%d",&v[j]);
hash[v[j]]=;
}
for (int j=;j<k;j++) {
if (isclique==) break;
for (int l=j+;l<k;l++)
if (g[v[j]][v[l]]==) {
isclique=;
printf ("Not a Clique\n");
break;
}
}
if (isclique==) continue;
for (int j=;j<=nv;j++) {
if (hash[j]==) {
for (int l=;l<k;l++) {
if (g[v[l]][j]==) break;
if (l==k-) isMaximal=;
}
}
if (isMaximal==) {
printf ("Not Maximal\n");
break;
}
}
if (isMaximal==) printf ("Yes\n");
}
return ;
}

A1146 Topological Order(25)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
const int inf=1e9;
int main () {
int N,M;
vector<int> g[maxn];
int inDegree[maxn];
scanf ("%d %d",&N,&M);
int u,v;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
g[u].push_back(v);
inDegree[v]++;
}
int a[maxn],t[maxn];
int k;
scanf ("%d",&k);
vector<int> vi;
for (int i=;i<=k;i++) {
for (int j=;j<=N;j++)
t[j]=inDegree[j];
int flag=,x;
for (int j=;j<=N;j++) {
scanf ("%d",&x);
if (t[x]!=) flag++;
for (int w=;w<g[x].size();w++)
t[g[x][w]]--;
}
if (flag!=) vi.push_back(i);
}
for (int i=;i<vi.size();i++) {
if (i!=) printf (" ");
printf ("%d",vi[i]-);
}
return ;
}

专题十  并查集

A1034 Head of Gang(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int father[maxn],isRoot[maxn]={},weight[maxn];
unordered_map<string,int> pos1;
unordered_map<int,string> pos2;
unordered_map<string,int> ans;
struct node {
string id;
int total=;
int num=;
}Node[maxn];
struct gang {
string id;
int num;
};
void init () {
for (int i=;i<maxn;i++)
father[i]=i;
}
int findfather (int x) {
int a=x;
while (x!=father[x])
x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union (int a,int b) {
int faA=findfather(a);
int faB=findfather(b);
if (faA!=faB) {
if (weight[faA]>weight[faB]) father[faB]=faA;
else father[faA]=faB;
}
}
bool cmp (gang a,gang b) {
return a.id<b.id;
}
int main () {
int N,K;
scanf ("%d %d",&N,&K);
init ();
string s1,s2;
int cnt=,x;
vector<pair<string,string>> v1;
for (int i=;i<N;i++) {
cin>>s1>>s2>>x;
if (pos1[s1]==) {
pos1[s1]=cnt;
pos2[cnt++]=s1;
}
if (pos1[s2]==) {
pos1[s2]=cnt;
pos2[cnt++]=s2;
}
weight[pos1[s1]]+=x;
weight[pos1[s2]]+=x;
v1.push_back({s1,s2});
}
for (int i=;i<v1.size();i++)
Union (pos1[v1[i].first],pos1[v1[i].second]);
for (int i=;i<cnt;i++) {
Node[findfather(i)].total+=weight[i];
Node[findfather(i)].num++;
}
for (int i=;i<cnt;i++) {
if (Node[i].total>K*&&Node[i].num>)
ans[pos2[i]]=Node[i].num;
}
printf ("%d\n",ans.size());
vector<gang> vi;
for (auto it=ans.begin();it!=ans.end();it++)
vi.push_back({it->first,it->second});
sort (vi.begin(),vi.end(),cmp);
for (int i=;i<vi.size();i++)
cout<<vi[i].id<<" "<<vi[i].num<<endl;
return ;
}

A1107 Social Clusters(30)

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
int father[maxn];
int isRoot[maxn];
int course[maxn];
int N;
void init () {
for (int i=;i<=N;i++) {
father[i]=i;
isRoot[i]=;
course[i]=;
}
}
int findfather (int x) {
int a=x;
while (x!=father[x])
x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union (int a,int b) {
int faA=findfather(a);
int faB=findfather(b);
if (faA!=faB) father[faA]=faB;
}
int main () {
scanf ("%d",&N);
init ();
int k,hobby;
for (int i=;i<=N;i++) {
scanf ("%d: ",&k);
for (int j=;j<k;j++) {
scanf ("%d",&hobby);
if (course[hobby]==) course[hobby]=i;
Union (i,course[hobby]);
}
}
for (int i=;i<=N;i++)
isRoot[findfather(i)]++;
int ans=;
for (int i=;i<=N;i++)
if (isRoot[i]!=) ans++;
printf ("%d\n",ans);
sort (isRoot+,isRoot+N+);
for (int i=N;i>=N-ans+;i--) {
printf ("%d",isRoot[i]);
if (i>N-ans+) printf (" ");
}
return ;
}

A1114 Family Property(25)

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
struct people {
int id,fatherid,motherid,num,area;
int child[];
}p[maxn];
struct family {
int id,renshu;
double num,area;
bool flag=false;
}adj[maxn];
bool visit[maxn]={false};
int father[maxn];
void init () {
for (int i=;i<maxn;i++)
father[i]=i;
}
int findfather (int x) {
int a=x;
while (x!=father[x])
x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union (int a,int b) {
int faA=findfather (a);
int faB=findfather (b);
if (faA>faB) father[faA]=faB;
if (faA<faB) father[faB]=faA;
}
bool cmp1 (family a,family b) {
if (a.area!=b.area) return a.area>b.area;
else return a.id<b.id;
}
int main () {
int N,cnt=,k;
init ();
scanf ("%d",&N);
for (int i=;i<N;i++) {
scanf ("%d %d %d %d",&p[i].id,&p[i].fatherid,&p[i].motherid,&k);
visit[p[i].id]=true;
if (p[i].fatherid!=-) {
visit[p[i].fatherid]=true;
Union (p[i].fatherid,p[i].id);
}
if (p[i].motherid!=-) {
visit[p[i].motherid]=true;
Union (p[i].motherid,p[i].id);
}
for (int j=;j<k;j++) {
scanf ("%d",&p[i].child[j]);
visit[p[i].child[j]]=true;
Union (p[i].id,p[i].child[j]);
}
scanf ("%d %d",&p[i].num,&p[i].area);
}
for (int i=;i<N;i++) {
int id=findfather(p[i].id);
adj[id].id=id;
adj[id].num+=p[i].num;
adj[id].area+=p[i].area;
adj[id].flag=true;
}
for (int i=;i<maxn;i++)
if (visit[i])
adj[findfather(i)].renshu++;
for (int i=;i<maxn;i++)
if (adj[i].flag) {
cnt++;
adj[i].area=(double)(adj[i].area*1.0/adj[i].renshu);
adj[i].num=(double)(adj[i].num*1.0/adj[i].renshu);
}
printf ("%d\n",cnt);
sort (adj,adj+maxn,cmp1);
for (int i=;i<cnt;i++)
printf ("%04d %d %.3f %.3f\n",adj[i].id,adj[i].renshu,adj[i].num,adj[i].area);
return ; }

A1118 Birds in Forest(25)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
int father[maxn];
int isRoot[maxn]={};
int visit[maxn]={};
void init () {
for (int i=;i<maxn;i++)
father[i]=i;
}
int findfather (int x) {
int a=x;
while (x!=father[x])
x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union (int a,int b) {
int faA=findfather(a);
int faB=findfather(b);
father[faA]=faB;
}
int main () {
int N;
scanf ("%d",&N);
int maxnum=-;
int K,x,p;
init ();
for (int i=;i<N;i++) {
scanf ("%d %d",&K,&x);
visit[x]++;
if (x>maxnum) maxnum=x;
for (int j=;j<K;j++) {
scanf ("%d",&p);
if (p>maxnum) maxnum=p;
Union (p,findfather(x));
}
}
vector<int> vi;
int q,u,v;
scanf ("%d",&q);
for (int i=;i<q;i++) {
scanf ("%d %d",&u,&v);
if (findfather(u)!=findfather(v)) vi.push_back();
else vi.push_back();
}
int ans=;
for (int i=;i<=maxnum;i++)
isRoot[findfather(i)]++;
for (int i=;i<=maxnum;i++)
if (isRoot[i]!=) ans++;
printf ("%d %d\n",ans,maxnum);
for (int i=;i<vi.size();i++) {
if (vi[i]==) printf ("No\n");
else printf ("Yes\n");
}
return ;
}

A1119 Pre-and Post-order Traversals(30)

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
int pre[maxn],post[maxn];
vector<int> in;
bool unique1=true;
void inOrder (int preL,int preR,int postL,int postR) {
if (preL==preR) {
in.push_back(pre[preL]);
return;
}
if (pre[preL]==post[postR]) {
int i=preL+;
while (i<=preR&&pre[i]!=post[postR-]) i++;
if (i-preL>)
inOrder (preL+,i-,postL,postL+i-preL-);
else unique1=false;
in.push_back(post[postR]);
inOrder (i,preR,postL+i-preL-,postR-);
}
}
int main () {
int N;
scanf ("%d",&N);
for (int i=;i<N;i++)
scanf ("%d",&pre[i]);
for (int i=;i<N;i++)
scanf ("%d",&post[i]);
inOrder (,N-,,N-);
if (unique1==true) printf ("Yes\n");
else printf ("No\n");
//printf ("%d",in[0]);
for (int i=;i<N;i++) {
if (i!=) printf (" ");
printf ("%d",in[i]);
}
printf ("\n");
//printf ("%d",in[i]);
return ;
}

专题十一  平方探测法

A1078 Hashing(25)

#include<iostream>
using namespace std;
int size,n,hashTable[];
bool isprim (int num) {
if (num==) return false;
for (int i=;i*i<=num;i++)
if (num%i==) return false;
return true;
}
void insert (int key) {
for (int step=;step<size;step++) {
int index1=(key+step*step)%size;
if (hashTable[index1]==) {
hashTable[index1]=;
printf ("%d",index1%size);
return;
}
}
printf ("-");
}
int main () {
scanf ("%d %d",&size,&n);
while (!isprim(size)) size++;
for (int i=;i<n;i++) {
int key;
scanf ("%d",&key);
if (i!=) printf (" ");
insert (key);
}
return ;
}

A1145 Hashing-AST(25)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
bool isprime (int n) {
for (int i=;i*i<=n;i++)
if (n%i==) return false;
return true;
}
int main () {
int tsize,n,m,a;
scanf ("%d %d %d",&tsize,&n,&m);
while (!isprime(tsize)) tsize++;
vector<int> v(tsize);
for (int i=;i<n;i++) {
scanf ("%d",&a);
int flag=;
for (int j=;j<tsize;j++) {
int pos=(a+j*j)%tsize;
if (v[pos]==) {
v[pos]=a;
flag=;
break;
}
}
if (flag==) printf ("%d cannot be inserted.\n",a);
}
int ans=;
for (int i=;i<m;i++) {
scanf ("%d",&a);
for (int j=;j<=tsize;j++) {
ans++;
int pos=(a+j*j)%tsize;
if (v[pos]==a||v[pos]==) break;
}
}
printf ("%.1f\n",ans*1.0/m);
return ;
}

专题十二  模拟+杂题

A1002 A+B for Polynomials(25)

#include<cstdio>
int main () {
double num[]={};
int K,front;
double behind;
scanf ("%d",&K);
for (int i=;i<K;i++) {
scanf ("%d %lf",&front,&behind);
num[front]+=behind;
}
scanf ("%d",&K);
for (int i=;i<K;i++) {
scanf ("%d %lf",&front,&behind);
num[front]+=behind;
}
int ans=;
for (int i=;i>=;i--)
if (num[i]!=) ans++;
printf ("%d",ans);
for (int i=;i>=;i--)
if (num[i]!=) printf (" %d %.1f",i,num[i]);
return ;
}

A1009 Product of Polynomials(25)

#include<cstdio>
int main () {
double product[]={};
double a1[]={},a2[]={};
int n1,q;
scanf ("%d",&n1);
for (int i=;i<n1;i++) {
scanf ("%d",&q);
scanf ("%lf",&a1[q]);
}
int n2;
scanf ("%d",&n2);
for (int i=;i<n2;i++) {
scanf ("%d",&q);
scanf ("%lf",&a2[q]);
}
for (int i=;i<=;i++) {
for (int j=;j<=;j++) {
product[i+j]+=a1[i]*a2[j];
}
}
int ans=;
for (int i=;i<=;i++)
if (product[i]!=0.0) ans++;
printf ("%d",ans);
for (int i=;i>=;i--)
if (product[i]!=0.0) printf (" %d %.1f",i,product[i]);
return ;
}

A1011 World Cup Betting(20)

#include<cstdio>
int main (){
double w[],t[],l[],a[],r;
for (int i=;i<;i++)
scanf ("%lf %lf %lf",&w[i],&t[i],&l[i]);
for (int i=;i<;i++){
if (w[i]>t[i]&&w[i]>l[i]) {
printf ("W ");
a[i]=w[i];
}
if (t[i]>w[i]&&t[i]>l[i]) {
printf ("T ");
a[i]=t[i];
}
if (l[i]>t[i]&&l[i]>w[i]) {
printf ("L ");
a[i]=l[i];
}
}
r=(a[]*a[]*a[]*0.65-)*;
printf ("%.2f",r);
return ;
}

A1014 Waiting in Line(30)

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxNode=;
int n,m,k,query,q;
int convertToMinute (int h,int m) {
return h*+m;
}
struct Window {
int endTime,popTime;
queue<int> q;
}window[];
int ans[maxNode],needTime[maxNode];
int main () {
int inIndex=;
scanf ("%d%d%d%d",&n,&m,&k,&query);
for (int i=;i<k;i++)
scanf ("%d",&needTime[i]);
for (int i=;i<n;i++)
window[i].popTime=window[i].endTime=convertToMinute(,);
for (int i=;i<min(n*m,k);i++) {
window[inIndex%n].q.push(inIndex);
window[inIndex%n].endTime+=needTime[inIndex];
if (inIndex<n) window[inIndex].popTime=needTime[inIndex];
ans[inIndex]=window[inIndex%n].endTime;
inIndex++;
}
for (;inIndex<k;inIndex++) {
int idx=-,minPopTime=<<;
for (int i=;i<n;i++) {
if (window[i].popTime<minPopTime) {
idx=i;
minPopTime=window[i].popTime;
}
}
Window& W=window[idx];
W.q.pop();
W.q.push(inIndex);
W.endTime+=needTime[inIndex];
W.popTime+=needTime[W.q.front()];
ans[inIndex]=W.endTime;
}
for (int i=;i<query;i++) {
scanf ("%d",&q);
if (ans[q-]-needTime[q-]>=convertToMinute(,))
printf ("Sorry\n");
else
printf ("%02d:%02d\n",ans[q-]/,ans[q-]%);
}
return ;
}

A1015 Reversible Primes(20)

#include<cstdio>
#include<cmath>
bool isPrime (int n) {
if (n<=) return false;
int sqr=(int)sqrt(1.0*n);
for (int i=;i<=sqr;i++)
if (n%i==) return false;
return true;
}
int d[];
int main () {
int n,radix;
while (scanf ("%d",&n)!=EOF) {
if (n<) break;
scanf ("%d",&radix);
if (isPrime(n)==false)
printf ("No\n");
else {
int len=;
do {
d[len++]=n%radix;
n/=radix;
}while (n!=);
for (int i=;i<len;i++)
n=n*radix+d[i];
if (isPrime(n)==true) printf ("Yes\n");
else printf ("No\n");
}
}
return ;
}

A1017 Queueing at  Bank(25)

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int K=;
const int INF=;
struct Customer {
int comeTime,serveTime;
}newCustomer;
vector<Customer> custom;
int convertTime (int h,int m,int s) {
return h*+m*+s;
}
bool cmp (Customer a,Customer b) {
return a.comeTime<b.comeTime;
}
int endTime[K];
int main () {
int c,w,totTime=;
int stTime=convertTime(,,);
int edTime=convertTime(,,);
scanf ("%d%d",&c,&w);
for (int i=;i<w;i++) endTime[i]=stTime;
for (int i=;i<c;i++) {
int h,m,s,serveTime;
scanf ("%d:%d:%d %d",&h,&m,&s,&serveTime);
int comeTime=convertTime(h,m,s);
if (comeTime>edTime) continue;
newCustomer.comeTime=comeTime;
newCustomer.serveTime=serveTime<=?serveTime*:;
custom.push_back(newCustomer);
}
sort(custom.begin(),custom.end(),cmp);
for (int i=;i<custom.size();i++) {
int idx=-,minEndTime=INF;
for (int j=;j<w;j++) {
if (endTime[j]<minEndTime) {
minEndTime=endTime[j];
idx=j;
}
}
if (endTime[idx]<=custom[i].comeTime) {
endTime[idx]=custom[i].comeTime+custom[i].serveTime;
}
else {
totTime+=(endTime[idx]-custom[i].comeTime);
endTime[idx]+=custom[i].serveTime;
}
}
if (custom.size()==) printf ("0.0");
else printf ("%.1f",totTime/60.0/custom.size());
return ;
}

A1019 General Palindromic Number(20)

#include<cstdio>
bool Judge (int z[],int num) {
for (int i=;i<=num/;i++) {
if (z[i]!=z[num--i])
return false;
}
return true;
}
int main () {
int n,b,z[],num=;
scanf ("%d%d",&n,&b);
do {
z[num++]=n%b;
n/=b;
} while (n!=);
bool flag=Judge(z,num);
if (flag==true) printf ("Yes\n");
else printf ("No\n");
for (int i=num-;i>=;i--) {
printf ("%d",z[i]);
if (i!=) printf (" ");
}
return ;
}

A1025 PAT Ranking(25)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct student {
char id[];
int score;
int location_number;
int local_rank;
}stu[];
bool cmp (student a,student b) {
if (a.score!=b.score) return a.score>b.score;
else return strcmp(a.id,b.id)<;
}
int main () {
int n,k,num=;
scanf ("%d",&n);
for (int i=;i<=n;i++) {
scanf ("%d",&k);
for (int j=;j<k;j++) {
scanf ("%s %d",stu[num].id,&stu[num].score);
stu[num].location_number=i;
num++;
}
sort (stu+num-k,stu+num,cmp);
stu[num-k].local_rank=;
for (int j=num-k+;j<num;j++) {
if (stu[j].score==stu[j-].score) {
stu[j].local_rank=stu[j-].local_rank;
}
else {
stu[j].local_rank=j+-(num-k);
}
}
}
printf ("%d\n",num);
sort (stu,stu+num,cmp);
int r=;
for (int i=;i<num;i++) {
if (i>&&stu[i].score!=stu[i-].score) {
r=i+;
}
printf ("%s ",stu[i].id);
printf ("%d %d %d\n",r,stu[i].location_number,stu[i].local_rank);
}
return ;
}

A1027 Colors in Mars(20)

#include<cstdio>
char radix[]={'','','','','','','','','','','A','B','C'};
int main () {
int r,g,b;
scanf ("%d%d%d",&r,&g,&b);
printf ("#");
printf ("%c%c",radix[r/],radix[r%]);
printf ("%c%c",radix[g/],radix[g%]);
printf ("%c%c",radix[b/],radix[b%]);
return ;
}

A1029 Medians(25)

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main () {
vector<int> vi;
int n;
scanf ("%d",&n);
for (int i=;i<n;i++) {
int w;
scanf ("%d",&w);
vi.push_back(w);
}
int m;
scanf ("%d",&m);
for (int i=;i<m;i++) {
int j;
scanf ("%d",&j);
vi.push_back(j);
}
sort (vi.begin(),vi.begin()+vi.size());
if (vi.size()%==) printf ("%d\n",vi[vi.size()/-]);
else printf ("%d\n",vi[vi.size()/]);
return ;
}

A1031 Hello World for U(20)

#include<cstdio>
#include<cstring>
int main () {
char s[];
int w=;
while ((s[w]=getchar())!='\n')
w++;
s[w]='\0';
int N=strlen(s);
int n1=(N+)/;
int n3=n1;
int n2=N+-n1*;
for (int i=;i<n1-;i++) {
printf ("%c",s[i]);
for (int j=;j<n2-;j++)
printf (" ");
printf ("%c\n",s[N-i-]);
}
for (int i=;i<n2;i++)
printf ("%c",s[n1+i-]);
return ;
}

A1032 Sharing(25)

#include<cstdio>
using namespace std;
struct node {
char key;
int next;
bool flag;
}node[];
int main () {
int s1,s2,n,a,b;
scanf ("%d%d%d",&s1,&s2,&n);
char data;
for (int i=;i<n;i++) {
scanf ("%d %c %d",&a,&data,&b);
node[a]={data,b,false};
}
for (int i=s1;i!=-;i=node[i].next)
node[i].flag=true;
for (int i=s2;i!=-;i=node[i].next)
if (node[i].flag==true) {
printf ("%05d",i);
return ;
}
printf ("-1");
return ;
}

A1033 To Fill or Not to Fill

#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
struct station {
double price,dis;
};
bool cmp (station a,station b) {
return a.dis<b.dis;
}
int main () {
double cmax,d,davg;
int n;
scanf ("%lf%lf%lf%d",&cmax,&d,&davg,&n);
vector<station> sta(n+);
sta[]={0.0,d};
for (int i=;i<=n;i++) {
scanf ("%lf%lf",&sta[i].price,&sta[i].dis);
}
sort (sta.begin(),sta.end(),cmp);
double nowdis=0.0,maxdis=0.0,nowprice=0.0,totalPrice=0.0,leftdis=0.0;
if (sta[].dis!=) {
printf ("The maximum travel distance = 0.00");
return ;
}
else nowprice=sta[].price;
while (nowdis<d) {
maxdis=nowdis+cmax*davg;
double minPriceDis=,minPrice=inf;
int flag=;
for(int i=;i<=n&&sta[i].dis<=maxdis;i++) {
if (sta[i].dis<=nowdis) continue;
if (sta[i].price<nowprice) {
totalPrice+=(sta[i].dis-nowdis-leftdis)*nowprice/davg;
leftdis=0.0;
nowprice=sta[i].price;
nowdis=sta[i].dis;
flag=;
break;
}
if(sta[i].price<minPrice) {
minPrice=sta[i].price;
minPriceDis=sta[i].dis;
}
}
if(flag==&&minPrice!=inf) {
totalPrice+=(nowprice*(cmax-leftdis/davg));
leftdis=cmax*davg-(minPriceDis-nowdis);
nowprice=minPrice;
nowdis=minPriceDis;
}
if(flag == && minPrice == inf) {
nowdis+=cmax*davg;
printf("The maximum travel distance = %.2f",nowdis);
return ;
}
}
printf("%.2f",totalPrice);
return ;
}

A1037 Magic Coupon(25)

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
bool cmp (int a,int b) {
return a>b;
}
int main () {
vector<int> pos1,mns1,pos2,mns2;
int a[],b[];
int Na,Nb;
scanf ("%d",&Na);
for (int i=;i<Na;i++) {
scanf ("%d",&a[i]);
if (a[i]>) pos1.push_back(a[i]);
if (a[i]<) mns1.push_back(a[i]);
}
scanf ("%d",&Nb);
for (int i=;i<Nb;i++) {
scanf ("%d",&b[i]);
if (b[i]>) pos2.push_back(b[i]);
if (b[i]<) mns2.push_back(b[i]);
}
sort (pos1.begin(),pos1.begin()+pos1.size(),cmp);
sort (pos2.begin(),pos2.begin()+pos2.size(),cmp);
sort (mns1.begin(),mns1.begin()+mns1.size());
sort (mns2.begin(),mns2.begin()+mns2.size());
int len1=min(pos1.size(),pos2.size());
int len2=min(mns1.size(),mns2.size());
int sum=;
for (int i=;i<len1;i++)
sum+=pos1[i]*pos2[i];
for (int i=;i<len2;i++)
sum+=mns1[i]*mns2[i];
printf ("%d",sum);
return ; }

A1038 Recover the Smallest Number(30)

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool cmp (string a,string b) {
return a+b<b+a;
}
int main () {
string a[];
int N;
scanf ("%d",&N); for (int i=;i<N;i++)
cin>>a[i];
sort(a,a+N,cmp);
string ans;
for (int i=;i<N;i++)
ans+=a[i];
while (ans.size()!=&&ans[]=='')
ans.erase(ans.begin());
if (ans.size()==) cout<< ;
else cout<< ans;
return ;
}

A1039 Course Lisr for Student(25)

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N=;
const int M=***+;
vector<int> selectCourse[M];
int getID (char name[]) {
int id=;
for (int i=;i<;i++)
id=id*+(name[i]-'A');
id=id*+(name[]-'');
return id;
}
int main () {
char name[];
int n,k;
scanf ("%d%d",&n,&k);
for (int i=;i<k;i++) {
int course,x;
scanf ("%d%d",&course,&x);
for (int j=;j<x;j++) {
scanf ("%s",name);
int id=getID(name);
selectCourse[id].push_back(course);
}
}
for (int i=;i<n;i++) {
scanf ("%s",name);
int id=getID(name);
sort(selectCourse[id].begin(),selectCourse[id].end());
printf ("%s %d",name,selectCourse[id].size());
for (int j=;j<selectCourse[id].size();j++)
printf (" %d",selectCourse[id][j]);
printf ("\n");
}
return ;
}

A1040 Longest Symmetric String(25)

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int main () {
char s[];
int w=;
while ((s[w]=getchar())!='\n')
w++;
s[w]='\0';
int maxlen=;
int len;
vector<char> t1,t2;
for (int i=;i<w;i++) {
for (int j=w-;j>=;j--) {
if (s[i]==s[j]) {
int l=i,r=j;
int flag=;
while (l<r) {
l++;r--;
if (s[l]!=s[r]) {
flag++;break;
}
}
if (flag) continue;
len=j-i+;
if (len>maxlen) maxlen=len;
}
}
}
printf ("%d",maxlen);
return ;
}

A1041 Be Unique(20)

#include<cstdio>
using namespace std;
int main () {
int HashTable[]={};
int N;
scanf ("%d",&N);
int a[],i;
for (i=;i<N;i++) {
scanf ("%d",&a[i]);
HashTable[a[i]]++;
}
int ans=-;
for (i=;i<N;i++)
if (HashTable[a[i]]==) {
ans=a[i];
break;}
if (ans==-) printf ("None");
else printf ("%d",ans);
return ; }

A1042 Shuffling Machine(20)

#include<cstdio>
char hs[]={'S','H','C','D','J'};
int main () {
int start[];
int end[];
for (int i=;i<=;i++)
start[i]=i;
int K;
scanf ("%d",&K);
int op[];
for (int i=;i<=;i++)
scanf ("%d",&op[i]);
while (K--) {
for (int i=;i<=;i++)
end[op[i]]=start[i];
for (int i=;i<=;i++)
start[i]=end[i];
}
for (int i=;i<=;i++)
{
start[i]--;
if (i!=) printf ("%c%d ",hs[start[i]/],start[i]%+);
else printf ("%c%d",hs[start[i]/],start[i]%+);
}
return ;
}

A1046 Shortest Distance(20)

#include<cstdio>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
int main ()
{
int dis[]={};
int N,q;
vector<int> s;
scanf ("%d",&N);
dis[]=;
for (int i=;i<=N;i++) {
scanf ("%d",&q);
dis[i+]=dis[i]+q;
}
int T,d1,d2;
scanf ("%d",&T);
for (int i=;i<T;i++) {
scanf ("%d %d",&d1,&d2);
int temp=fabs(dis[d1]-dis[d2]);
s.push_back(min(temp,dis[N+]-temp));
}
for (int i=;i<s.size();i++)
printf ("%d\n",s[i]);
return ;
}

A1047 Student List for Course(25)

#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
char name[][];
vector<int> course[];
bool cmp1(int a, int b) {
return strcmp(name[a], name[b]) < ;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for(int i = ; i < n; i++) {
int cnt, temp;
scanf("%s %d", name[i], &cnt);
for(int j = ; j < cnt; j++) {
scanf("%d", &temp);
course[temp].push_back(i);
}
}
for(int i = ; i <= k; i++) {
printf("%d %d\n", i, course[i].size());
sort(course[i].begin(), course[i].end(), cmp1);
for(int j = ; j < course[i].size(); j++)
printf("%s\n", name[course[i][j]]);
}
return ;
}

A1048 Find Coins(25)

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int HashTable[N]={};
int main () {
int n,m,a;
scanf ("%d%d",&n,&m);
for (int i=;i<n;i++) {
scanf ("%d",&a);
++HashTable[a];
}
for (int i=;i<m;i++) {
if (HashTable[i]&&HashTable[m-i]) {
if (i==m-i&&HashTable[i]<=) continue;
printf ("%d %d\n",i,m-i);
return ;
}
}
printf ("No Solution\n");
return ; }

A1049 Counting Ones(30)

#include <iostream>
using namespace std;
int main() {
int n, left = , right = , a = , now = , ans = ;
scanf("%d", &n);
while(n / a) {
left = n / (a * ), now = n / a % , right = n % a;
if(now == ) ans += left * a;
else if(now == ) ans += left * a + right + ;
else ans += (left + ) * a;
a = a * ;
}
printf("%d", ans);
return ;
}

A1050 String Substractoin(20)

#include<cstdio>
#include<cstring>
using namespace std;
int main () {
bool HashTable[]={false};
char s1[];
int w=;
while ((s1[w]=getchar())!='\n')
w++;
s1[w]='\0';
char c;
while ((c=getchar())!='\n')
HashTable[c]=true;
for (int i=;i<strlen(s1);i++)
if (HashTable[s1[i]]==false)
printf ("%c",s1[i]);
return ;
}

A1051 Pop Sequence(25)

#include<cstdio>
#include<stack>
using namespace std;
const int maxn=;
int arr[maxn];
stack<int> st;
int main () {
int m,n,T;
scanf ("%d%d%d",&m,&n,&T);
while (T--) {
while (!st.empty()) st.pop();
for (int i=;i<=n;i++)
scanf ("%d",&arr[i]);
int current=;
bool flag=true;
for (int i=;i<=n;i++) {
st.push(i);
if (st.size()>m) {
flag=false;
break;
}
while (!st.empty()&&st.top()==arr[current]) {
st.pop();
current++;
}
}
if (st.empty()==true&&flag==true)
printf ("YES\n");
else printf ("NO\n");
}
return ;
}

A1052 Linked List Sorting(25)

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
struct Node {
int address,data,next;
bool flag;
}node[maxn];
bool cmp (Node a,Node b) {
if (a.flag==false||b.flag==false)
return a.flag>b.flag;
else return a.data<b.data;
}
int main () {
for (int i=;i<maxn;i++)
node[i].flag=false;
int n,begin,address;
scanf ("%d %d",&n,&begin);
for (int i=;i<n;i++) {
scanf ("%d",&address);
scanf ("%d %d",&node[address].data,&node[address].next);
node[address].address=address;
}
int cnt=,p=begin;
while (p!=-) {
node[p].flag=true;
cnt++;
p=node[p].next;
}
if (cnt==) printf ("0 -1");
else {
sort (node,node+maxn,cmp);
printf ("%d %05d\n",cnt,node[].address);
for (int i=;i<cnt;i++){
if (i!=cnt-) printf ("%05d %d %05d\n",node[i].address,node[i].data,node[i+].address);
else printf ("%05d %d -1\n",node[i].address,node[i].data);
}
}
return ;
}

A1055 The World Richest(25)

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
struct node {
char name[];
int age, money;
};
int cmp1(node a, node b) {
if(a.money != b.money)
return a.money > b.money;
else if(a.age != b.age)
return a.age < b.age;
else
return (strcmp(a.name, b.name) < );
} int main() {
int n, k, num, amin, amax;
scanf("%d %d", &n, &k);
vector<node> vt(n), v;
vector<int> book(, );
for(int i = ; i < n; i++)
scanf("%s %d %d", vt[i].name, &vt[i].age, &vt[i].money);
sort(vt.begin(), vt.end(), cmp1);
for(int i = ; i < n; i++) {
if(book[vt[i].age] < ) {
v.push_back(vt[i]);
book[vt[i].age]++;
}
}
for(int i = ; i < k; i++) {
scanf("%d %d %d", &num, &amin, &amax);
vector<node> t;
for(int j = ; j < v.size(); j++) {
if(v[j].age >= amin && v[j].age <= amax)
t.push_back(v[j]);
}
if(i != ) printf("\n");
printf("Case #%d:", i + );
int flag = ;
for(int j = ; j < num && j < t.size(); j++) {
printf("\n%s %d %d", t[j].name, t[j].age, t[j].money);
flag = ;
}
if(flag == ) printf("\nNone");
}
return ;
}

A1056 Mice and Rice(25)

#include<cstdio>
#include<queue>
using namespace std;
const int maxn=;
struct mouse {
int weight;
int R;
}mouse[maxn];
int main () {
int np,ng,order;
scanf ("%d%d",&np,&ng);
for (int i=;i<np;i++)
scanf ("%d",&mouse[i].weight);
queue<int> q;
for (int i=;i<np;i++) {
scanf ("%d",&order);
q.push(order);
}
int temp=np,group;
while (q.size()!=) {
if (temp%ng==) group=temp/ng;
else group=temp/ng+;
for (int i=;i<group;i++) {
int k=q.front();
for (int j=;j<ng;j++) {
if (i*ng+j>=temp) break;
int front=q.front();
if (mouse[front].weight>mouse[k].weight)
k=front;
mouse[front].R=group+;
q.pop();
}
q.push(k);
}
temp=group;
}
mouse[q.front()].R=;
for (int i=;i<np;i++) {
printf ("%d",mouse[i].R);
if (i<np-) printf (" ");
}
return ;
}

A1058 A+B in Hogwarts(20)

#include<cstdio>
int main () {
int x1,x2,x3,y1,y2,y3,z1=,z2=,z3=;
scanf ("%d.%d.%d %d.%d.%d",&x1,&x2,&x3,&y1,&y2,&y3);
z3=x3+y3;z2=x2+y2;z1=x1+y1;
if (z3>=) {
z3-=;
z2+=;
}
if (z2>=) {
z2-=;
z1+=;
}
printf ("%d.%d.%d",z1,z2,z3);
return ;
}

A1059 Prime Factors(25)

#include<cstdio>
#include<cmath>
const int maxn=;
bool is_prime(int n) {
if (n==) return false;
int sqr=(int)sqrt(1.0*n);
for (int i=;i<=sqr;i++)
if (n%i==) return false;
return true;
}
int prime[maxn],pNum=;
void Find_prime () {
for (int i=;i<maxn;i++)
if (is_prime(i)==true)
prime [pNum++]=i;
}
struct Factor {
int x,cnt;
}fac[];
int main () {
Find_prime ();
int n,num=;
scanf ("%d",&n);
if (n==) printf ("1=1");
else {
printf ("%d=",n);
int sqr=(int)sqrt(1.0*n);
for (int i=;i<pNum&&prime[i]<=sqr;i++) {
if (n%prime[i]==) {
fac[num].x=prime[i];
fac[num].cnt=;
while (n%prime[i]==) {
fac[num].cnt++;
n/=prime[i];
}
num++;
}
if (n==) break;
}
if (n!=) {
fac[num].x=n;
fac[num++].cnt=;
}
for (int i=;i<num;i++) {
if (i>) printf ("*");
printf ("%d",fac[i].x);
if (fac[i].cnt>)
printf ("^%d",fac[i].cnt);
}
}
return ;
}

A1062 Talent and Virture(25)

#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
struct people{
int talent;
int virtue;
int id;
int flag;
};
bool cmp(people a,people b){
if(a.flag==b.flag){
if((a.virtue+a.talent)==(b.virtue+b.talent)){
if(a.virtue==b.virtue){
return a.id<b.id;
}else{
return a.virtue>b.virtue;
}
}else{
return a.virtue+a.talent>b.virtue+b.talent;
}
}else{
return a.flag<b.flag;
}
} int main(){
int N,L,H;
while(scanf("%d%d%d",&N,&L,&H)!=EOF){
vector<people> vp;
people input;
for(int i=;i<N;i++){
scanf("%d%d%d",&input.id,&input.virtue,&input.talent);
if(input.virtue>=H&&input.talent>=H){// sages
input.flag = ;
}else if((input.talent<H&&input.talent>=L)&&input.virtue>=H){// noblemen
input.flag = ;
}else if((input.virtue<H&&input.virtue>=L) &&
(input.talent<H&&input.talent>=L) &&
(input.virtue>=input.talent)){// foolmen
input.flag = ;
}else if(input.virtue>=L&&input.talent>=L){// the rest
input.flag = ;
}else{
continue;
}
vp.push_back(input);
}
sort(vp.begin(),vp.end(),cmp);
printf("%d\n",vp.size());
for(auto elem:vp){
printf("%08d %d %d\n",elem.id,elem.virtue,elem.talent);
}
}
return ;
}

A1065 A+B and C(20)

#include <cstdio>
using namespace std;
int main() {
int n;
scanf("%d", &n);
for(int i = ; i < n; i++) {
long long a, b, c;
scanf("%lld %lld %lld", &a, &b, &c);
long long sum = a + b;
if(a > && b > && sum < ) {
printf("Case #%d: true\n", i + );
}
else if (a < && b < && sum >= ) {
printf("Case #%d: false\n", i + );
}
else if (sum > c) {
printf("Case #%d: true\n", i + );
}
else {
printf("Case #%d: false\n", i + );
}
}
return ;
}

A1067 Sort with Swap(25)

#include<bits/stdc++.h>
using namespace std;
int main () {
int n,t,cnt=,a[];
scanf ("%d",&n);
for (int i=;i<n;i++) {
scanf ("%d",&t);
a[t]=i;
}
for (int i=;i<n;i++) {
if (i!=a[i]) {
while (a[]!=) {
swap(a[a[]],a[]);
cnt++;
}
if (i!=a[i]) {
swap (a[],a[i]);
cnt++;
}
}
}
printf ("%d\n",cnt);
return ;
}

A1068 Find More Coins(30)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int dp[],w[];
bool choice[][];
int cmp1 (int a,int b) {
return a>b;
}
int main () {
int n,m;
scanf ("%d %d",&n,&m);
for (int i=;i<=n;i++)
scanf ("%d",&w[i]);
sort (w+,w+n+,cmp1);
for (int i=;i<=n;i++) {
for (int j=m;j>=w[i];j--) {
if (dp[j]<=dp[j-w[i]]+w[i]) {
choice[i][j]=true;
dp[j]=dp[j-w[i]]+w[i];
}
}
}
if (dp[m]!=m) printf ("No Solution");
else {
vector<int> arr;
int v=m,index1=n;
while (v>) {
if (choice[index1][v]==true) {
arr.push_back(w[index1]);
v-=w[index1];
}
index1--;
}
for (int i=;i<arr.size();i++) {
if (i!=) printf (" ");
printf ("%d",arr[i]);
}
}
return ;
}

A1070 Mooncake(25)

#include<cstdio>
#include<algorithm>
using namespace std;
struct mooncake {
double store;
double sell;
double price;
}a[];
bool cmp (mooncake a,mooncake b) {
return a.price>b.price;
}
int main () {
int N;double D;
scanf ("%d %lf",&N,&D);
for (int i=;i<N;i++)
scanf ("%lf",&a[i].store);
for (int i=;i<N;i++)
scanf ("%lf",&a[i].sell);
for (int i=;i<N;i++)
a[i].price=a[i].sell/a[i].store;
sort(a,a+N,cmp);
double sum=,profit=;
for (int i=;i<N;i++) {
if (D-sum>=a[i].store) {
profit+=a[i].sell;
sum+=a[i].store;
}
else {
profit+=a[i].price*(D-sum);
break;
}
}
printf ("%.2f",profit);
return ;
}

A1081 Rational Sum(20)

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
struct fraction {
ll up,down;
};
ll gcd(ll a,ll b) {
if (b==) return a;
else return gcd(b,a%b);
}
fraction reduction (fraction result) {
if (result.down<) {
result.up=-result.up;
result.down=-result.down;
}
if (result.up==) result.down=;
else {
int d=gcd(abs(result.up),abs(result.down));
result.up/=d;
result.down/=d;
}
return result;
}
fraction add (fraction fa,fraction fb) {
fraction result;
result.up=fa.up*fb.down+fb.up*fa.down;
result.down=fa.down*fb.down;
return reduction(result);
}
void showResult (fraction r) {
//reduction(r);
if (r.down==) printf ("%lld",r.up);
else if (abs(r.up)>r.down)
printf ("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down);
else printf ("%lld/%lld",r.up,r.down);
}
int main () {
int N;
scanf ("%d",&N);
fraction r1,r2,sum;
sum.up=;
sum.down=;
for (int i=;i<N;i++) {
scanf ("%lld/%lld",&r1.up,&r1.down);
sum=add(sum,r1);
}
showResult(sum);
return ;
}

A1082 Read Number in Chinese(25)

#include<cstdio>
#include<cstring>
char num[][]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
char wei[][]={"Shi","Bai","Qian","Wan","Yi"};
int main () {
char str[];
int w=;
while ((str[w]=getchar())!='\n')
w++;
str[w]='\0';
int len=strlen(str);
int left=,right=len-;
if (str[]=='-') {
printf ("Fu");
left++;
}
while (left+<=right)
right-=;
while (left<len) {
bool flag=false;
bool isPrint=false;
while (left<=right) {
if (left>&&str[left]=='')
flag=true;
else {
if (flag==true) {
printf (" ling");
flag=false;
}
if (left>) printf (" ");
printf ("%s",num[str[left]-'']);
isPrint=true;
if (left!=right)
printf (" %s",wei[right-left-]);
}
left++;
}
if (isPrint==true&&right!=len-)
printf (" %s",wei[(len--right)/+]);
right+=;
}
return ;
}

A1083 List Grades(25)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct stu {
char name[];
char id[];
int grade;
};
int cmp1(stu a, stu b) {
return a.grade > b.grade;
}
int main() {
int n, low, high, cnt = ;
scanf("%d", &n);
vector<stu> v(n);
for(int i = ; i < n; i++) {
scanf("%s %s %d", v[i].name, v[i].id, &v[i].grade);
}
scanf("%d %d", &low, &high);
for(int i = ; i < n; i++) {
if(v[i].grade < low || v[i].grade > high) {
v[i].grade = -;
} else {
cnt++;
}
}
sort(v.begin(), v.end(), cmp1);
for(int i = ; i < cnt; i++) {
printf("%s %s\n", v[i].name, v[i].id);
}
if(cnt == )
printf("NONE");
return ;
}

A1084 Broken Keyboard(20)

#include<cstdio>
#include<cstring>
using namespace std;
int main () {
char s1[],s2[];
int w=;
bool HashTable[]={false};
while ((s1[w]=getchar())!='\n')
w++;
s1[w]='\0';
w=;
while ((s2[w]=getchar())!='\n')
w++;
s2[w]='\0';
int len1=strlen(s1),len2=strlen(s2);
int i,j;
for (i=;i<len1;i++) {
for (j=;j<len2;j++) {
if (s1[i]>='a'&&s1[i]<='z')
s1[i]-=;
if (s2[j]>='a'&&s2[j]<='z')
s2[j]-=;
if (s1[i]==s2[j]) break;
}
if (j==len2&&HashTable[s1[i]]==false) {
HashTable[s1[i]]=true;
printf ("%c",s1[i]);
}
}
return ;
}

A1085 Perfect Sequence(25)

#include<cstdio>
#include<algorithm>
using namespace std;
int main () {
int N,P;
scanf ("%d %d",&N,&P);
int a[];
for (int i=;i<N;i++)
scanf ("%d",&a[i]);
sort(a,a+N);
int ans=,maxans=;
for (int i=;i<N-;i++) {
int j=upper_bound(a+i+,a+N,(long long)a[i]*P)-a;
ans=max(ans,j-i);
}
printf ("%d",ans);
return ;
}

A1092 To Buy or Not To Buy(20)

#include<cstdio>
#include<cstring>
using namespace std;
int main () {
char s1[],s2[];
int w=;
while ((s1[w]=getchar())!='\n')
w++;
s1[w]='\0';
w=;
while ((s2[w]=getchar())!='\n')
w++;
s2[w]='\0';
int len1=strlen(s1);
int len2=strlen(s2);
int HashTable1[]={},HashTable2[]={};
for (int i=;i<len1;i++)
HashTable1[s1[i]]++;
for (int i=;i<len2;i++)
HashTable2[s2[i]]++;
int flag=;
for (int i=;i<;i++) {
if (HashTable1[i]<HashTable2[i]) {
flag+=HashTable2[i]-HashTable1[i];
}
}
if (flag==) {
printf ("Yes %d",strlen(s1)-strlen(s2));
}
else printf ("No %d",flag);
return ; }