0
点赞
收藏
分享

微信扫一扫

周中练习1 C

苦茶如歌 2022-08-10 阅读 51


A friend of yours has written a program that compares every pair of a list of items. With n items,

it works as follows. First, it prints a 1, and it compares item 1 to items 2, 3, 4, . . . , n. It then prints


a 2, and compares item 2 to items 3, 4, 5, . . . , n. It continues like that until every pair of items has


been compared exactly once. If it compares item x to item y, it will not later compare item y to


item x. Also, it does not compare any item to itself.


Your friend wants to know when his program is halfway done. For a program that makes an odd


number of total comparisons, this is when it is doing the middle comparison. For a program that


makes an even number of total comparisons, this is when it is doing the first of the two middle


comparisons.


What will the last number printed be when the program is halfway done?


Note that since the earlier items have more comparisons than the later items, the answer is not


simply n/2.


Input


The input consists of a single line containing the integer n (2 ≤ n ≤ 109


).


Output


Print, on a single line, the last number your friend’s program prints when it is halfway done.


Sample Input and Output

4 1
7 2
10 3
1919 562290976843 85225144


思路:

一个二分的题目;

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=110;
long long work(long long x)
{
long long sum=x*(x+1)/2;
return sum;

}
int main()
{
int n;
scanf("%d",&n);
long long x=n-1;
long long sum=x*(x+1)/2;
long long mm;
if(sum%2==0)
{
mm=(sum)/2;
}
else mm=(sum+1)/2;
long long l=1,r=n-1;
while(l<=r)
{
long long mid=(l+r)/2;
long long mi=n-mid-1;
if(sum-work(mi)<mm)
{
l=mid+1;
}
else
{
r=mid-1;
}
}
printf("%I64d\n",l);
return 0;
}



举报

相关推荐

0 条评论