ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

poj 1852 Ants

2021-07-07 11:31:56  阅读:278  来源: 互联网

标签:int max 1852 max1 cin Ants poj min1 include


http://poj.org/problem?id=1852

题目大意

army 陆军,大军

reside 居住

一群蚂蚁走在长为 1 c m 1cm 1cm的水平杆上,每个的速度是 1 c m / s 1cm/s 1cm/s

当走到尽头,掉下去。

当两只碰到一起,那么两只往反方向走。

知道它们的初始位置,不知道原始蚂蚁的移动方向。

求蚁群全部掉下去的最早和最晚时间


题解

最小值:找到一个比较靠近中心的蚂蚁,让它往最近的终点掉落的时间

最大值:找到一个蚂蚁,它距离对面的终点最远,让它掉落的时间

先排序(数据是无序的)


代码

//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define all(a) (a).begin(),(a).end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef vector<int> vi;

int n,p,tmp,a1,a2;

int main() {
    int t; cin >> t;
    while(t--){
        cin >> p >> n; 
        vi a;
        rep(i,1,n) cin >> tmp, a.pb(tmp);
        sort(all(a));
        rep(i,0,n-1){
            a1=max(a1,min(a[i],p-a[i]));
            a2=max(a2,max(a[i],p-a[i]));
        }
        //a2 = max(p-a[0],a[n-1]); 如果是这样的话,p=200,a[0]=190,a[n-1]=195,不成立
        cout << a1 << " " << a2 << endl;
    }
    return 0;
}


代码二

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    int n,l,t;
    cin >> t;
    while(t--)
    {
        int min1 = 0,max1 = 0,x;
        cin >> l >> n;
        for(int i = 0;i < n;i++)
        {
            cin >> x;
            if(x <= l / 2)
            {
                min1 = max(min1,x);
                max1 = max(max1,l - x);
            }
            else
            {
                min1 = max(min1,l - x);
                max1 = max(max1,x);
            }
        }
        cout << min1 << " " << max1 << endl;
    }
    return 0;
}
// https://www.cnblogs.com/biaobiao88/p/11832757.html

标签:int,max,1852,max1,cin,Ants,poj,min1,include
来源: https://blog.csdn.net/luker6/article/details/118540567

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有