百度之星资格赛 J题 百度的新大厦

来源:互联网 发布:架构图制作软件 mac 编辑:程序博客网 时间:2024/04/29 03:44

思路:直接解方程就可以了。设电梯一次上升a,一次下降b,总共按按钮n次,设按上升按钮按了x次,则最后电梯的位置是a*x-b*(n-x),求满足该式的最小正数值即可。总共有2000个电梯,枚举每个电梯,找最小值即可。

ac代码:

  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <string.h>  
  4.   
  5. using namespace std;  
  6.   
  7. struct house{  
  8.     int up,down;  
  9. }hh[2012];  
  10.   
  11. int fun(int x,int y,int n){  
  12.     int num = (y*n) / (x+y);  
  13.     int mod = (y*n) % (x+y);  
  14.     if(mod == 0){  
  15.       return x+y;  
  16.     }  
  17.     else{  
  18.       int ans = (num+1) * (x+y) - (y*n);  
  19.       return ans;  
  20.     }  
  21. }  
  22.   
  23. int main(){  
  24.     int n,m;  
  25.     while(~scanf("%d%d",&n,&m)){  
  26.         int x;  
  27.         int ans = 100000000;  
  28.         for(int i = 0;i < m;++i){  
  29.             scanf("%d%d",&hh[i].up,&hh[i].down);  
  30.             x = fun(hh[i].up,hh[i].down,n);  
  31.             if(x<ans)  
  32.                 ans = x;  
  33.         }  
  34.         printf("%d\n",ans);  
  35.     }  
  36.     return 0;  
  37. }  
0 0
原创粉丝点击