winter 2024 day5
SMU 2024 winter round1
7-1最好的文档
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 //#define int __int128 5 #define double long double 6 typedef pair<int,int>PII; 7 typedef pair<string,int>PSI; 8 typedef pair<string,string>PSS; 9 const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; 10 const int MAXN=1e8+5; 11 const double eps=1e-12; 12 void solve() { 13 cout<<"Good code is its own best documentation."; 14 } 15 16 signed main(){ 17 ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 18 int t=1; 19 // cin>>t; 20 while(t--){ 21 solve(); 22 } 23 return 0; 24 }
7-2自动编程
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 //#define int __int128 5 #define double long double 6 typedef pair<int,int>PII; 7 typedef pair<string,int>PSI; 8 typedef pair<string,string>PSS; 9 const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; 10 const int MAXN=1e8+5; 11 const double eps=1e-12; 12 void solve() { 13 int n;cin>>n; 14 cout<<"print("<<n<<")"; 15 } 16 17 signed main(){ 18 ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 19 int t=1; 20 // cin>>t; 21 while(t--){ 22 solve(); 23 } 24 return 0; 25 }
7-3程序员买包子
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; void solve() { int n,m,k; string x; cin>>n>>x>>m>>k; if(k==n)cout<<"mei you mai "<<x<<" de"; else if(k==m)cout<<"kan dao le mai "<<x<<" de"; else cout<<"wang le zhao mai "<<x<<" de"; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-4猜数字-交互版
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 //#define int __int128 5 #define double long double 6 typedef pair<int,int>PII; 7 typedef pair<string,int>PSI; 8 typedef pair<string,string>PSS; 9 const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; 10 const int MAXN=1e8+5; 11 const double eps=1e-12; 12 void solve() { 13 int n; 14 cin>>n; 15 int l=1,r=n,x; 16 while(l<=r){ 17 int mid=l+r>>1; 18 cout<<mid<<'\n'; 19 string s; 20 cin>>s; 21 if(s=="<")r=mid-1; 22 else l=mid+1,x=mid; 23 } 24 cout<<"! "<<x<<'\n'; 25 } 26 27 signed main(){ 28 // ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 29 int t=1; 30 // cin>>t; 31 while(t--){ 32 solve(); 33 } 34 return 0; 35 }
7-5斯德哥尔摩火车上的题
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 //#define int __int128 5 #define double long double 6 typedef pair<int,int>PII; 7 typedef pair<string,int>PSI; 8 typedef pair<string,string>PSS; 9 const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; 10 const int MAXN=1e8+5; 11 const double eps=1e-12; 12 void solve() { 13 string a,b; 14 cin>>a>>b; 15 auto P=[](string a){ 16 string s=""; 17 for (int i = 1; i < a.size(); i++) { 18 if (a[i] % 2 == a[i-1] % 2) { 19 s += max(a[i], a[i-1]); 20 } 21 } 22 // s="www.multisoft.se/"+s; 23 return s; 24 }; 25 a=P(a),b=P(b); 26 if(a==b)cout<<a; 27 else cout<<a<<'\n'<<b; 28 } 29 30 signed main(){ 31 // ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 32 int t=1; 33 // cin>>t; 34 while(t--){ 35 solve(); 36 } 37 return 0; 38 }
7-6剪切粘贴
思路:模拟每一步
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; void solve() { string s; cin>>s; int n; cin>>n; while(n--){ int l,r; string a,b; cin>>l>>r>>a>>b; string c=s.substr(l-1,r-l+1); string now=s.substr(0,l-1)+s.substr(r,s.size()-r); string pp=a+b; int p=now.find(pp); if(p>=0&&p<now.size()){ now.insert(p+a.size(),c); }else now+=c; s=now; } cout<<s; } signed main(){ // ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-7天梯赛的善良
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; void solve() { int n; cin>>n; map<int,int>mp; for(int i=0;i<n;++i){ int x; cin>>x; mp[x]++; } cout<<mp.begin()->first<<' '<<mp.begin()->second<<'\n'; cout<<mp.rbegin()->first<<' '<<mp.rbegin()->second; } signed main(){ // ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-8谷歌的招聘
思路:枚举每个k位数判断是否为素数
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; void solve() { int n,k; cin>>n>>k; string s; cin>>s; string a; auto P=[](int n){ if(n==1||n==0)return false; for(int i=2;i*i<=n;++i){ if(n%i==0)return false; } return true; }; for(int i=0;i<s.size();++i){ a.push_back(s[i]); if(a.size()==k){ int b= stoi(a); if(P(b)){ cout<<a; return ; } a.erase(a.begin()); } } cout<<404; } signed main(){ // ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-9锦标赛
思路:看成满二叉树,给了每局的败者分数a,a要满足不小于左右子树的最大值(即使是该局败者,但也是其子树的胜者),找到左右子树对应的位置,有空位则插入即可。若a小于左右子树的最大值,说明无解
下图为每个节点对应到一维数组中的位置范围
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; const int dx[4]={-1,1,0,0}; const int dy[4]={0,0,-1,1}; void solve() { int k;cin>>k; vector<int>ans(1<<k),f(1<<k); for(int i=k-1;i>=0;--i){ for(int j=0;j<1<<i;++j){ int x; cin>>x; bool ok=false; if(f[j*2]<=x){ for(int p=(j*2)*(1<<k-i-1);p<(j*2+1)*(1<<k-i-1);++p){ if(!ans[p]){ ans[p]=x; ok=true; break; } } } if(!ok&&f[j*2+1]<=x){ for(int p=(j*2+1)*(1<<k-i-1);p<(2*j+2)*(1<<k-i-1);++p){ if(!ans[p]){ ans[p]=x; ok=true; break; } } } if(!ok){ cout<<"No Solution\n"; return ; } f[j]=max({x,f[j*2],f[j*2+1]}); } } int x; cin>>x; if(f[0]<=x){ for(int i=0;i<1<<k;++i){ if(!ans[i])ans[i]=x; cout<<ans[i]; if(i<(1<<k)-1)cout<<' '; } }else cout<<"No Solution"; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-10插松枝
思路:模拟题,细心点
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 //#define int __int128 5 #define double long double 6 typedef pair<int,int>PII; 7 typedef pair<string,int>PSI; 8 typedef pair<string,string>PSS; 9 const int N=3e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; 10 const int MAXN=1e8+5; 11 const double eps=1e-12; 12 void solve() { 13 int n,m,k; 14 cin>>n>>m>>k; 15 vector<vector<int>>ans; 16 stack<int>st,now; 17 vector<int>ve(n+5); 18 for(int i=0;i<n;++i)cin>>ve[i]; 19 auto P=[&](){ 20 vector<int>g; 21 while(now.size()){ 22 g.push_back(now.top()); 23 now.pop(); 24 } 25 std::reverse(g.begin(), g.end()); 26 ans.push_back(g); 27 }; 28 for(int i=0;i<n;){ 29 if(now.size()==k)P(); 30 if(now.size()==0){ 31 if(st.empty()){ 32 now.push(ve[i++]); 33 } 34 else { 35 now.push(st.top()),st.pop(); 36 } 37 continue; 38 } 39 if(!st.empty()&&st.top()<=now.top()){ 40 now.push(st.top());st.pop(); 41 continue; 42 } 43 if(ve[i]<=now.top()){ 44 now.push(ve[i++]); 45 continue; 46 }else{ 47 while(ve[i]>now.top()&&i<n&&st.size()<m){ 48 st.push(ve[i++]); 49 } 50 if(i<n&&ve[i]<=now.top()){ 51 now.push(ve[i++]); 52 continue; 53 } 54 } 55 if(now.size())P(); 56 } 57 while(st.size()){ 58 if(now.size()==k)P(); 59 60 if(now.empty()){ 61 now.push(st.top()),st.pop(); 62 continue; 63 }else{ 64 if(st.top()<=now.top()){ 65 now.push(st.top()),st.pop(); 66 continue; 67 } 68 } 69 if(now.size())P(); 70 } 71 if(now.size())P(); 72 for(int i=0;i<ans.size();++i){ 73 cout<<ans[i][0]; 74 for(int j=1;j<ans[i].size();++j)cout<<' '<<ans[i][j]; 75 if(i<ans.size()-1)cout<<'\n'; 76 } 77 } 78 79 signed main(){ 80 // ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 81 int t=1; 82 // cin>>t; 83 while(t--){ 84 solve(); 85 } 86 return 0; 87 }
7-11拯救007
思路:数据也不大,也比较水,dfs、bfs都可以
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; int n,d; bool ok=false; vector<int>st(N); vector<PII>ve; int dis(PII a,PII b){ double s=(a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); return s; } bool can(PII a,PII b,double x){ double s=(a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); if(s>x*x)return false; return true; } bool is(int u){ if(50-ve[u].first<=d||ve[u].first+50<=d)return true; if(50-ve[u].second<=d||ve[u].second+50<=d)return true; return false; } void dfs(int u){ if(is(u)){ ok=true; return ; } st[u]=1; for(int i=0;i<n;++i){ if(!st[i]&&can(ve[i],ve[u],d)){ dfs(i); } } } void solve() { cin>>n>>d; PII o={0,0}; for(int i=0;i<n;++i){ int x,y; cin>>x>>y; ve.push_back({x,y}); if(dis(ve[i],o)*1.0<=7.5*7.5)st[i]=1; } if(d*1.0>=42.5){ cout<<"Yes"; return ; } for(int i=0;i<n;++i){ if(ok)break; if(!st[i]&&can(o,ve[i],d+7.5)){ dfs(i); } } if(ok)cout<<"Yes"; else cout<<"No"; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-12寻宝图
思路:dfs或bfs
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; const int dx[4]={-1,1,0,0}; const int dy[4]={0,0,-1,1}; void solve() { int n,m;cin>>n>>m; vector<string>s(n+5); for(int i=1;i<=n;++i){ cin>>s[i]; s[i]=' '+s[i]; } int a=0,b=0; for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ bool ok=false; if(s[i][j]>='1'&&s[i][j]<='9'){ a++; if(s[i][j]>'1')ok=true; s[i][j]='a'; queue<PII>q; q.push({i,j}); while(q.size()){ auto t=q.front();q.pop(); int x=t.first,y=t.second; for(int k=0;k<4;++k){ int xx=x+dx[k],yy=y+dy[k]; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&s[xx][yy]>='1'&&s[xx][yy]<='9'){ q.push({xx,yy}); if(s[xx][yy]>'1')ok=true; s[xx][yy]='a'; } } } if(ok)b++; } } } cout<<a<<' '<<b; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-13垃圾箱分布
思路:垃圾箱数量不多,求每个垃圾箱到居民最短路
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; const double g=9.8; void solve() { cout<<"No Solution"; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-14非常弹的球
思路:代换下物理公式,分解下速度,vy=gt,s=vxt,E=mv2/2,vy=vsinθ,vx=vcosθ,一次起点到落地的距离x=2*s=2v2sinθvcosθ/g=v2sin2θ/g,当2θ为90度时x最大,由于v2=2E/m,每次改变v2即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; const double g=9.8; void solve() { double w,p,ans=0; cin>>w>>p; w/=100,p/=100; double E=1000; double x=2*E/w/g; while(x>eps){ ans+=x; E*=(1-p); x=2.0*E/w/g; } cout<<fixed<<setprecision(3)<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
7-15拯救007(升级版)
思路:求最短路径(跳的次数),注意忽略已经在岛上的点,由于有多解的情况可以先对第一跳的距离排序
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const int MAXN=1e8+5; const double eps=1e-12; int n,d; bool ok=false; vector<int>path,D; vector<PII>ve; int dis(PII a,PII b){ int s=(a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); return s; } bool can(PII a,PII b,double x){ int s=(a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); if(s*1.0>x*x)return false; return true; } bool is(int u){ if(ve[u].first-d<=-50||ve[u].first+d>=50)return true; if(ve[u].second-d<=-50||ve[u].second+d>=50)return true; return false; } void solve() { cin>>n>>d; PII o={0,0}; vector<PII>g; path=vector<int>(n,-1); D=vector<int>(n,INF); for(int i=0;i<n;++i){ int x,y; cin>>x>>y; ve.push_back({x,y}); if(dis(ve[i],o)*1.0>7.5*7.5)g.push_back({dis(ve[i],o),i}); } if(d*1.0>=42.5){ cout<<1; return ; } queue<int>q; sort(g.begin(),g.end()); for(int i=0;i<g.size();++i){ if(can(ve[g[i].second],o,d+7.5)){ // cout<<i<<":"<<dis(g[i],o)<<' '<<pow(7.5+d,2)<<'\n'; q.push(g[i].second),D[g[i].second]=1; path[g[i].second]=-1; } } while(q.size()){ auto t=q.front();q.pop(); if(is(t)){ stack<int>s; int p=t; while(p!=-1){ s.push(p); p=path[p]; } cout<<s.size()+1<<'\n'; while(s.size()){ int u=s.top();s.pop(); cout<<ve[u].first<<' '<<ve[u].second; if(s.size())cout<<'\n'; } return ; } for(int i=0;i<g.size();++i){ if(D[g[i].second]==INF&&can(ve[t],ve[g[i].second],d)){ D[g[i].second]=D[t]+1; path[g[i].second]=t; q.push(g[i].second); } } } cout<<0; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } return 0; }