丢手绢

丢手绢

https://ac.nowcoder.com/acm/problem/207040

丢手绢

解题思路
尺取法(双指针)
先计算距离和,即圆圈周长。之后定义两个指针i,j,用res表示最长距离,当i和j之间的距离大于等于周长的一半时,更新res的值。

AC代码

#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int a[N];
int sum;

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    int x=sum/2;
    int res=0;
    int cnt=0;
    int j=0;
    for(int i=0;i<n;i++)
    {
        while(cnt<x)
        {
            cnt+=a[(j++)%n];
        }
        res=max(res,min(cnt,sum-cnt));
        cnt-=a[i];
    }
    cout<<res<<endl;

    return 0;
}

全部评论
好像有点问题,第27行取最小函数没有意义啊
点赞 回复 分享
发布于 02-03 20:39 福建
写的不错
点赞 回复 分享
发布于 2023-06-12 19:06 江西

相关推荐

06-25 09:33
厦门大学 Java
程序员饺子:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司8个岗位
点赞 评论 收藏
分享
自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
评论
25
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务