2 条题解

  • 0
    @ 2024-10-16 11:02:41

    #include using namespace std;

    int n,l,r; int mas=0;

    int main(){

    cin>>n>>l>>r;
    
    for(int i=0;i<l;i++){
    	int x;
    	cin>>x;
    	if(x>mas)mas=x;
    }
    for(int i=0;i<r;i++){
    	int y;
    	cin>>y;
    	if(n-y>mas)mas=n-y;
    }
    
    cout<<mas<<endl;
    
    return 0;
    

    }

    • 0
      @ 2024-10-11 22:14:08

      题目要求的是最后一只蚂蚁从木板上掉下来的时刻,所以并不需要区分每只蚂蚁。注意到题目中的信息,由于改变移动方向不花费额外时间,而且改变移动方向后的移动速度不变,因此,两只相遇的蚂蚁同时改变移动方向之后的情形等价于两只蚂蚁都不改变移动方向,继续按照原来的方向和速度移动,这样问题就简化成根据每只蚂蚁的初始位置和移动方向得到最后一只蚂蚁到达木板边界的时刻。

      假设一只蚂蚁在位置 pp。如果这只蚂蚁向左移动,则它到达木板边界需要的时间是 pp。如果这只蚂蚁向右移动,则它到达木板边界需要的时间是 npn−p

      遍历数组 leftleftrightright,根据每只蚂蚁的初始位置和移动方向得到每只蚂蚁到达木板边界需要的时间,其中的最大值即为最后一只蚂蚁到达木板边界的时刻,也是最后一只蚂蚁从木板上掉下来的时刻。

      #include<bits/stdc++.h>
      using namespace std;
      int main(){
          int n,l,r;
          cin>>n>>l>>r;
          vector<int> left(l);
          vector<int> right(r);
          for(int i=0;i<l;i++)cin>>left[i];
          for(int i=0;i<r;i++)cin>>right[i];
          int lastMoment = 0;
          for (int ant : left) {
                  lastMoment = max(lastMoment, ant);
              }
          for (int ant : right) {
              lastMoment = max(lastMoment, n - ant);
              }
          cout<<lastMoment;
          return 0;
      }
      
      • 1

      信息

      ID
      114
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      60
      已通过
      18
      上传者