Clock Splitter+uvalive+二分

Clock Splitter
An analogue clock has the first twelve natural numbers placed in an equally spaced fashion in a clockwise
direction on its face. It is possible to draw one line across this regular clock such that the sum of the
numbers on both sides of the line are equal, as shown in figure A.
It is not possible to draw such a line for a clock with the first five natural numbers on its face.
However it is possible to draw a line such that the sum of the numbers on both sides of the line differ
by one, as shown in figure B.
Your task is to write a program to find where the line can be drawn for a clock with the first N
natural numbers such that the sum of the numbers on both sides of the line are as close as possible to
each other.
For some values of N, there will be more than one possible solution. Your program must report
only the line with the smallest starting number.
Input
The input contains a number of test cases with one input value N per case, 2 ≤ N ≤ 100000, which is
the largest value on the clock face.
A value of zero (0) on a line by itself indicates the end of input and should not be processed.
Output
The output consists of a single line, for each test case, which contains two integers separated by a single
space. The integers represent the smallest number followed by the largest number, in the clockwise
direction, on the same side of the splitting line.ACM-ICPC Live Archive: 6407 – Clock Splitter 2/2
Sample Input
12
5
13
0
Sample Output
4 9
3 4

1 9


解决方案:枚举起始点,在二分答案,时间复杂度n*log(n)

code:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
long long sum(long long st,long long en)
{
    return (st+en)*(en-st+1)/2;
}
int main()
{
    long long N;
    while(~scanf("%lld",&N)&&N)
    {
        long long Sum=(1+N)*N/2LL;
        long long st,en,diff=0x3f3f3f3f;

        for(int i=1; i<=N; i++)
        {
            int low=i,high=N,mid;
            while(low<high)
            {
                mid=low+(high-low)/2;
                if(sum(i,mid)-(Sum-sum(i,mid))>=0)
                {
                    high=mid;
                }
                else low=mid+1;
            }
            if(sum(i,low)==(Sum-sum(i,low)))
            {
                diff=0;
                st=i,en=low;
                break;
            }
            else
            {
                long long second=fabs(sum(i,low)-(Sum-sum(i,low)));
                long long first=fabs(sum(i,low-1)-(Sum-sum(i,low-1)));
                if(second==first&&second<diff)
                {
                    diff=second;
                    st=i,en=low;
                }
                else
                {
                    if(second<first&&second<diff)
                    {
                        diff=second;
                        st=i,en=low;
                    }
                    else if(first<second&&first<diff)
                    {
                        diff=first;
                        st=i,en=low-1;
                    }
                }
            }
        }
        printf("%lld %lld\n",st,en);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值