十月天梯赛训练补题 10.31
A - a HDU - 4442
解题思路:假设有两个队伍a和b,如先去排a,则总体等待时间为:
ax+ax*by+bx,如先去排b,则总体的等待时间为bx+bx*ay+ax。
我们假设a在前优,则:ax*by<ay*bx。由此推广到多个队伍,按照ax*by<ay*bx的标准排序后所得的顺序处理即为答案。
代码:
1 #include <set> 2 #include <map> 3 #include <list> 4 #include <stack> 5 #include <queue> 6 #include <deque> 7 #include <cmath> 8 #include <string> 9 #include <vector> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <sstream> 14 #include <iostream> 15 #include <algorithm> 16 //#include <unordered_map> 17 #define INF 0x3f3f3f3f 18 #define ll long long 19 #define ull unsigned long long 20 #define FILL(a,n,v) fill(a,a+n,v) 21 #define Mset(a,v) memset(a,v,sizeof a) 22 #define Mcpy(a,b) memcpy(a,b,sizeof b) //a=b 23 #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) 24 using namespace std; 25 const int mod=365*24*60*60; 26 const int maxn=1e5+10; 27 int n; 28 struct node 29 { 30 ll a; 31 ll b; 32 }arr[maxn]; 33 34 bool cmp(node a,node b) 35 { 36 return a.a*b.b<b.a*a.b; 37 } 38 39 int main() 40 { 41 while(cin>>n&&n) 42 { 43 for(int i=0;i<n;i++) cin>>arr[i].a>>arr[i].b; 44 sort(arr,arr+n,cmp); 45 ll t=0; 46 for(int i=0;i<n;i++) 47 { 48 t+=arr[i].a+t*arr[i].b; 49 t%=mod; 50 } 51 cout<<t<<endl; 52 } 53 }
B - b HDU - 3791
题意:
给出输入序列判断是否为同一棵二叉搜索树。
代码:
1 #include <iostream> 2 #include <queue> 3 #include <stack> 4 using namespace std; 5 const int maxn=1e5+10;; 6 7 struct node 8 { 9 int value; 10 node *l,*r; 11 12 node() 13 { 14 this->l=NULL; 15 this->r=NULL; 16 } 17 18 node(int v) 19 { 20 this->value=v; 21 this->l=NULL; 22 this->r=NULL; 23 } 24 }; 25 26 class binarySearchTree 27 { 28 public: 29 node* root; 30 /** 31 二叉搜索树建树 32 @a 输入数组 a[0]为根结点 33 @n 数组a的长度 34 */ 35 void buildTree(int a[],int n) 36 { 37 this->root=new node(a[0]); 38 for(int i=1;i<n;i++) 39 { 40 node *now=new node(a[i]); 41 node *fa=this->root; 42 while(1) 43 { 44 if(now->value<fa->value) 45 { 46 if(fa->l==NULL) 47 { 48 fa->l=now; 49 break; 50 } 51 else fa=fa->l; 52 } 53 else 54 { 55 if(fa->r==NULL) 56 { 57 fa->r=now; 58 break; 59 } 60 else fa=fa->r; 61 } 62 } 63 } 64 } 65 /** 66 层序遍历序列 67 返回一个数组 68 */ 69 int* getLevelOrder() 70 { 71 int* a=new int[maxn]; 72 int cnt=0; 73 queue<node>q; 74 q.push(*this->root); 75 while(!q.empty()) 76 { 77 node now=q.front(); 78 a[cnt++]=now.value; 79 q.pop(); 80 if(now.l) q.push(*now.l); 81 if(now.r) q.push(*now.r); 82 } 83 return a; 84 } 85 /** 86 先序遍历序列 87 返回一个数组 88 */ 89 int* getPreOrder() 90 { 91 int* a=new int[maxn]; 92 int cnt=0; 93 node *now=this->root; 94 stack<node*>st; 95 while(now||st.size()) 96 { 97 while(now) 98 { 99 a[cnt++]=now->value; 100 st.push(now); 101 now=now->l; 102 } 103 if(st.size()) 104 { 105 now=st.top(); 106 st.pop(); 107 now=now->r; 108 } 109 } 110 return a; 111 } 112 /** 113 中序遍历序列 114 返回一个数组 115 */ 116 int* getInOrder() 117 { 118 int* a=new int[maxn]; 119 int cnt=0; 120 node *now=this->root; 121 stack<node*>st; 122 while(now||st.size()) 123 { 124 while(now) 125 { 126 st.push(now); 127 now=now->l; 128 } 129 if(st.size()) 130 { 131 now=st.top(); 132 st.pop(); 133 a[cnt++]=now->value; 134 now=now->r; 135 } 136 } 137 return a; 138 } 139 /** 140 后序遍历序列 141 返回一个数组 142 */ 143 int* getPostOrder() 144 { 145 int* a=new int[maxn]; 146 int cnt=0; 147 node* now=this->root; 148 stack<node*>st1; 149 stack<node*>st2; 150 while(now||st1.size()) 151 { 152 while(now) 153 { 154 st1.push(now); 155 st2.push(now); 156 now=now->r; 157 } 158 if(st1.size()) 159 { 160 now=st1.top(); 161 st1.pop(); 162 now=now->l; 163 } 164 } 165 while(st2.size()) 166 { 167 now=st2.top(); 168 st2.pop(); 169 a[cnt++]=now->value; 170 } 171 return a; 172 } 173 }; 174 175 int main() 176 { 177 int n; 178 while(cin>>n&&n) 179 { 180 string s; 181 cin>>s; 182 int a[maxn]; 183 int b[maxn]; 184 for(int i=0;i<s.size();i++) a[i]=s[i]-'0'; 185 186 binarySearchTree t; 187 t.buildTree(a,(int)s.size()); 188 int* pre1=t.getLevelOrder(); 189 190 while(n--) 191 { 192 cin>>s; 193 for(int i=0;i<s.size();i++) b[i]=s[i]-'0'; 194 t.buildTree(b,(int)s.size()); 195 int* pre2=t.getLevelOrder(); 196 197 bool flag=true; 198 for(int i=0;i<s.size();i++) 199 { 200 if(pre1[i]!=pre2[i]) 201 { 202 flag=false; 203 break; 204 } 205 } 206 if(flag) cout<<"YES"<<endl; 207 else cout<<"NO"<<endl; 208 } 209 210 211 } 212 213 return 0; 214 }
E - e HDU - 5938
题意:
给定一个字符串s
要求将s分成5个部分 并按顺序+-*/插入 使得最终的值最大
思路:因为有-,所以*和/后的结果要尽可能小,一位数*一位数最大只能是两位数,所以除数就可能为一位数或两位数,如果是三位数,那么会产生小数,从而浪费了前面的加和。还有就是前面相加的两个数可能是所有数+最后一位(相对而言)或第一位+后面的全部。四种情况比较一下大小即可。
1 #include <set> 2 #include <map> 3 #include <list> 4 #include <stack> 5 #include <queue> 6 #include <deque> 7 #include <cmath> 8 #include <string> 9 #include <vector> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <sstream> 14 #include <iostream> 15 #include <algorithm> 16 //#include <unordered_map> 17 #define INF 0x3f3f3f3f 18 #define ll long long 19 #define ull unsigned long long 20 #define FILL(a,n,v) fill(a,a+n,v) 21 #define Mset(a,v) memset(a,v,sizeof a) 22 #define Mcpy(a,b) memcpy(a,b,sizeof b) //a=b 23 #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) 24 using namespace std; 25 int n; 26 27 string s; 28 int a[30]; 29 30 ll solve(int l,int r) 31 { 32 ll ans=0; 33 for(int i=l;i<=r;i++) 34 { 35 ans=ans*10+a[i]; 36 } 37 return ans; 38 } 39 40 int main() 41 { 42 fcio; 43 cin>>n; 44 for(int k=1;k<=n;k++) 45 { 46 cin>>s; 47 int len=(int)s.size(); 48 for(int i=0;i<len;i++) a[i]=s[i]-'0'; 49 50 ll ans=a[0]+solve(1,len-4)-a[len-3]*a[len-2]/a[len-1]; 51 ans=max(ans,solve(0,len-5)+a[len-4]-a[len-3]*a[len-2]/a[len-1]); 52 if(len>5) 53 { 54 ll div=solve(len-2,len-1); 55 ans=max(ans,a[0]+solve(1,len-5)-a[len-4]*a[len-3]/div); 56 ans=max(ans,solve(0,len-6)+a[len-5]-a[len-4]*a[len-3]/div); 57 } 58 cout<<"Case #"<<k<<": "<<ans<<endl; 59 } 60 }
G - g HDU - 2680
多起点最短路
代码:
1 #include <set> 2 #include <map> 3 #include <list> 4 #include <stack> 5 #include <queue> 6 #include <deque> 7 #include <cmath> 8 #include <string> 9 #include <vector> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <sstream> 14 #include <iostream> 15 #include <algorithm> 16 //#include <unordered_map> 17 #define INF 0x3f3f3f3f 18 #define ll long long 19 #define ull unsigned long long 20 #define FILL(a,n,v) fill(a,a+n,v) 21 #define Mset(a,v) memset(a,v,sizeof a) 22 #define Mcpy(a,b) memcpy(a,b,sizeof b) //a=b 23 #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) 24 using namespace std; 25 const int maxn=1005; 26 27 int mp[maxn][maxn]; 28 bool vis[maxn]; 29 int dis[maxn]; 30 int n,m,s; 31 int u,v,c; 32 33 void init() 34 { 35 for(int i=0;i<=n;i++) 36 { 37 for(int j=0;j<=n;j++) 38 { 39 // if(i==j) mp[i][j]=0; 40 // else mp[i][j]=INF; 41 mp[i][j]=INF; 42 } 43 } 44 } 45 46 47 void dijkstra(int u) 48 { 49 memset(vis,false,sizeof(vis)); 50 for(int t=1;t<=n;t++) 51 { 52 dis[t]=mp[u][t]; 53 } 54 55 // for(int i=1;i<=n;i++) cout<<i<<':'<<dis[i]<<endl;// 56 57 vis[u]=true; 58 for(int t=1;t<n;t++) 59 { 60 int minn=INF; 61 int temp=0; 62 63 for(int i=1;i<=n;i++) 64 { 65 if(!vis[i]&&dis[i]<minn) 66 { 67 minn=dis[i]; 68 temp=i; 69 } 70 } 71 vis[temp]=true; 72 for(int i=1;i<=n;i++) 73 { 74 if(mp[temp][i]+dis[temp]<dis[i]) 75 { 76 dis[i]=mp[temp][i]+dis[temp]; 77 78 // cout<<temp<<' '<<i<<' '<<dis[i]<<endl; 79 80 } 81 } 82 } 83 } 84 85 int main() 86 { 87 fcio; 88 while(cin>>n>>m>>s) 89 { 90 init(); 91 while(m--) 92 { 93 cin>>v>>u>>c; 94 mp[u][v]=min(mp[u][v],c); 95 } 96 // for(int i=1;i<=n;i++) 97 // { 98 // for(int j=1;j<=n;j++) 99 // { 100 // cout<<i<<"->"<<j<<": "<<mp[i][j]<<endl; 101 // } 102 // } 103 dijkstra(s); 104 105 // for(int i=1;i<=n;i++) cout<<i<<':'<<dis[i]<<endl; 106 107 int minn=INF; 108 int q; 109 cin>>q; 110 while(q--) 111 { 112 int a; 113 cin>>a; 114 if(dis[a]<minn) minn=dis[a]; 115 } 116 if(minn==INF) cout<<-1<<endl; 117 else cout<<minn<<endl; 118 } 119 }
H - h HDU - 1873
优先队列模版题
代码:
#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <deque> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cstring> #include <cstdlib> #include <sstream> #include <iostream> #include <algorithm> //#include <unordered_map> #define INF 0x3f3f3f3f #define ll long long #define ull unsigned long long #define FILL(a,n,v) fill(a,a+n,v) #define Mset(a,v) memset(a,v,sizeof a) #define Mcpy(a,b) memcpy(a,b,sizeof b) //a=b #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) using namespace std; int n; struct node { int num; int pri; node(int num,int pri) { this->num=num; this->pri=pri; } bool operator<(const node& a) const { if(pri!=a.pri) return pri<a.pri; else return num>a.num; } }; int main() { while(cin>>n) { priority_queue<node>q[4]; string s; int i=1; for(int k=1;k<=n;k++) { cin>>s; if(s=="IN") { int a,b; cin>>a>>b; node pos(i,b); q[a].push(pos); i++; } else { int a; cin>>a; if(q[a].size()) { cout<<q[a].top().num<<endl; q[a].pop(); } else cout<<"EMPTY"<<endl; } } } }