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
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
- Clock Splitter+uvalive+二分
- UvaLive 6664 Clock Hands
- UVALive 6664 Clock Hands
- UvaLive--6664--Clock Hands【模拟】
- UVALive - 6269 Digital Clock 模拟
- UVALive 6269 Digital Clock --枚举,模拟
- UVALIVE 3635 Pie 二分
- UVALive 6144 Radiation 二分
- uvalive 2949(二分+贪心)
- UVALive - 3635 Pie 二分
- uvalive 4254(二分)
- uvalive 4725(贪心 + 二分)
- UVALive 6525 二分匹配
- UVALive 3635-Pie-二分
- UVALive 3177-贪心+二分
- UVALive 4254 二分
- UVALive 3635 Pie 【二分】
- UVALive 7261(二分)
- 小马哥-----高仿红米note 刷机救砖 各种主板拆机机型图面面观
- Java多线程生成文件性能分析案例
- 单元测试工具googletest C++Test和CppUnit
- 关于函数wait_event_interruptible(wq, condition)
- 乱码比较系统的解释
- Clock Splitter+uvalive+二分
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)
- g++编译命令选项
- Bugzilla
- XML简介
- Good Product Manager/Bad Product Manager 优秀的产品经理/不合格的产品经理
- MySql忘记密码的解决方法
- BaseSDK和DeploymentTarget究竟是什么意思?
- 实现统一时间格式的函数