Clock Splitter+uvalive+二分
2014-10-31 17:56
246 查看
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:
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; }
相关文章推荐
- UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)
- 【UVALive 4642】Malfatti Circles(圆,二分)
- uva 1421 & uvalive 4253 Archery(二分)
- UVALive 6442 Coins on a Ring(二分查找答案)
- uvalive 2949(二分+贪心)
- UVALive 7009 Secret Binary Tree(二分查找、dfs)
- UVALive 7500 Boxes and Balls 2015EC final 签到题 二分
- UVALive 3177 - 长城守卫(二分+贪心)
- uvalive 4725 - Airport (二分)
- uvalive 3635 - Pie(二分搜索)
- uvalive 4254(二分+模拟)
- UVALive 3177 长城守卫(贪心+二分)
- UVALive 3270 Simplified GSM Network 二分+Floyd
- UVALive 6611 Alice's Print Service(二分)
- UVALive - 4254 Processor 二分+优先队列
- UVA 12097 UVALive 3635 Pieni (二分)
- UVALive 3635-Pie-二分
- UVALive 3177-贪心+二分
- UVALive2678 UVA1121 Subsequence【前缀和+二分搜索+尺取法】
- UVALive 6902 Three Squares(二分 + 搜索)