Clock Splitter+uvalive+二分

来源:互联网 发布:淘宝女装进货渠道 编辑:程序博客网 时间:2024/06/06 05:36
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
原创粉丝点击