Benelux Algorithm Programming Contest 2014 Final
// Button Bashing (bfs)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <set> 6 #include <map> 7 #include <vector> 8 #include <cmath> 9 #include <queue> 10 using namespace std; 11 typedef long long ll; 12 const int inf=0x3f3f3f3f; 13 int T; 14 int a[50]; 15 //int d[8000];//数组的下标可以为负的 16 //d[i]表示 尽量凑成i需要的最小时间数目 17 map<int,int>mp;//用map更好 18 struct Node{ 19 int step,time; 20 }sta,ed,cnt,tp; 21 int main() 22 { 23 scanf("%d",&T); 24 while(T--) 25 { int n,t; 26 scanf("%d%d",&n,&t); 27 for(int i=0;i<n;i++) 28 scanf("%d",&a[i]); 29 //memset(d,inf,sizeof(d)); 30 for(int i=0;i<=3600;i++)//因为小于0为0,大于3600为3600 31 { 32 mp[i]=inf; 33 } 34 mp[0]=0; 35 // d[0]=0; 36 sta.step=0,sta.time=0; 37 ed.time=inf;//要求的 38 queue<Node>q; 39 while(!q.empty()){ 40 q.pop();//清空队列 41 } 42 q.push(sta); 43 while(!q.empty()){ 44 tp=q.front(); 45 q.pop(); 46 if(tp.time>=t){ //满足时间的 47 if(ed.time>tp.time) ed=tp;//先比时间,要尽量接近t 48 else if(ed.time==tp.time&&ed.step>tp.step) 49 ed=tp; 50 //时间考虑完后,要step尽量少 51 } 52 for(int i=0;i<n;i++){ 53 int nex=tp.time+a[i]; 54 if(nex<0) nex=0; 55 if(nex>3600) nex=3600; 56 // if(d[nex]>=inf){//最先到达nex的时间单元数目一定最少 57 if(mp[nex]>=inf){ 58 //d[nex]=d[tp.time]+1; 59 mp[nex]=mp[tp.time]+1; 60 // cnt.step=d[nex]; 61 cnt.step=mp[nex]; 62 cnt.time=nex; 63 q.push(cnt); 64 } 65 } 66 } 67 printf("%d %d\n",ed.step,ed.time-t); 68 } 69 70 return 0; 71 }
1 //Interesting Integers 2 /*对于一个n,它在新的数列里最多是第45项,因此可以遍历查找 3 a 4 b 5 a+b 6 a+2*b 7 2*a+3*b 8 3*a+5*b 9 有上面的规律可知 G[i]=f[i-2]*a+f[i-1]*b 10 我们要求的就是 a,b。 11 */ 12 #include <iostream> 13 #include <cstdio> 14 #include <cstring> 15 #include <algorithm> 16 #include <set> 17 #include <map> 18 #include <vector> 19 #include <cmath> 20 using namespace std; 21 typedef long long ll; 22 const int inf=0x3f3f3f3f; 23 int t; 24 ll f[56]; 25 void init() 26 { 27 f[1]=1,f[2]=1; 28 for(int i=3;i<=48;i++) 29 { 30 f[i]=f[i-2]+f[i-1]; 31 } 32 } 33 ll egcd(ll a,ll b,ll &x,ll &y) 34 { 35 ll d=a; 36 if(b==0){ 37 x=1; 38 y=0; 39 } 40 else{ 41 d=egcd(b,a%b,y,x); 42 y-=(a/b)*x; 43 } 44 return d; 45 } 46 //egcd求出的是a,b的最大公约数 47 int main() 48 { 49 scanf("%d",&t); 50 init(); 51 while(t--) 52 { ll n; 53 scanf("%lld",&n); 54 ll l=1,r=n; 55 for(int i=3;i<=45;i++)//45就已经大于10^9了 56 { 57 if(f[i]>n) break; 58 //G[i]=f[i-2]*a+f[i-1]*b,G[i]最小为f[i] 59 //G[i]等于n吗 60 ll x,y; 61 ll tmp=egcd(f[i-2],f[i-1],x,y); 62 if(n%tmp!=0) continue; 63 x*=n,y*=n;////a*x+b*y=1(egcd),是1 64 ll ans=(y-x)/f[i]; 65 x+=ans*f[i-1]; 66 y-=ans*f[i-2]; 67 //数学公式可推出:x,y都为(x*f[i-2]+y*f[i-1])/f[i] 68 if(x>y) { 69 x-=f[i-1];//多加了,减回去 70 y+=f[i-2]; 71 } 72 //上面的操作是为了让a,b,更接近 73 74 if(x<=0||y<=0) continue; 75 if(r>y) {//r要小 76 r=y; 77 l=x; 78 } 79 else if(r==y&&l>x){//r一样大时。l要小 80 l=x; 81 } 82 } 83 printf("%lld %lld\n",l,r); 84 } 85 return 0; 86 }
1 // Jury Jeopardy 2 //一道简单的模拟 3 #include <iostream> 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <set> 8 #include <map> 9 #include <vector> 10 #include <cmath> 11 using namespace std; 12 typedef long long ll; 13 const int inf=0x3f3f3f3f; 14 int t; 15 const int N=1e5+4; 16 char s[N]; 17 bool vis[500][500];//要大点 18 void change(int &x,int &y,int &d,char c){//x,y,d要用引用 19 //不能写成if If if 20 //例如 d==1,在第一个if中变成了2 21 //会在下个if 里继续操作 22 if(d==1){ 23 if(c=='R'){ 24 d=4; 25 x=x,y=y+1; 26 } 27 else if(c=='F'){ 28 d=1; 29 x=x+1,y=y; 30 } 31 else if(c=='L'){ 32 d=3; 33 x=x,y=y-1; 34 } 35 else{ 36 d=2; 37 x=x-1,y=y; 38 } 39 } 40 else if(d==2){ 41 if(c=='R'){ 42 d=3; 43 x=x,y=y-1; 44 } 45 else if(c=='F'){ 46 d=2; 47 x=x-1,y=y; 48 } 49 else if(c=='L'){ 50 d=4; 51 x=x,y=y+1; 52 } 53 else{ 54 d=1; 55 x=x+1,y=y; 56 } 57 } 58 else if(d==3){ 59 if(c=='R'){ 60 d=1; 61 x=x+1,y=y; 62 } 63 else if(c=='F'){ 64 d=3; 65 x=x,y=y-1; 66 } 67 else if(c=='L'){ 68 d=2; 69 x=x-1,y=y; 70 } 71 else{ 72 d=4; 73 x=x,y=y+1; 74 } 75 } 76 else{ 77 if(c=='R'){ 78 d=2; 79 x=x-1,y=y; 80 } 81 else if(c=='F'){ 82 d=4; 83 x=x,y=y+1; 84 } 85 else if(c=='L'){ 86 d=1; 87 x=x+1,y=y; 88 } 89 else{ 90 d=3; 91 x=x,y=y-1; 92 } 93 } 94 } 95 int main() 96 { 97 scanf("%d",&t); 98 int dir,x,y; 99 printf("%d\n",t); 100 while(t--){ 101 scanf("%s",&s); 102 dir=1,x=250,y=250; 103 int ymin=500; 104 int xmax=0,ymax=0; 105 memset(vis,0,sizeof(vis)); 106 vis[x][y]=1; 107 for(int i=0;i<strlen(s);i++){ 108 change(x,y,dir,s[i]); 109 vis[x][y]=1; 110 ymin=min(ymin,y); 111 xmax=max(xmax,x); 112 ymax=max(ymax,y); 113 } 114 ymin-=1;//适当变化,四周都是墙壁 115 ymax+=1; 116 xmax+=1; 117 printf("%d %d\n",ymax-ymin+1,xmax-250+1);//250一定最左 118 for(int i=ymin;i<=ymax;i++){//一行一行的输出 119 for(int j=250;j<=xmax;j++){ 120 if(vis[j][i]==1){//y对应i,往下变大,同理x往右变大。 121 printf("."); 122 } 123 else{ 124 printf("#"); 125 } 126 } 127 printf("\n"); 128 } 129 } 130 return 0; 131 }
分类:
江西多校
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现