hdu 2446 二分查找

这题本来应该是推公式做的,我一时没推出来,就全用二分查找了,打的时候没注意,提交的时候才发现ME,想了一想,杭电数据向来很弱的,就把MAXN改成1000000,过了,无语!

/*
* hdu2446/win.c
* Created on: 2011-8-20
* Author : ben
*/
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<math.h>

#define MAXN 5496092

long long array[MAXN], sum[MAXN];

void init() {
int i;
array[
0] = 1;
for (i = 1; i < MAXN; i++) {
array[i]
= i + 1 + array[i - 1];
}
sum[
0] = 1;
for (i = 1; i < MAXN; i++) {
sum[i]
= array[i] + sum[i - 1];
}
}

int findlayer(long long n) {
int low, high, mid;
low
= 0;
high
= MAXN - 1;
while (low <= high) {
mid
= (low + high) / 2;
if (sum[mid] == n) {
return mid;
}
else if (sum[mid] >= n) {
high
= mid - 1;
}
else {
low
= mid + 1;
}
}
if (sum[high] < n) {
return high + 1;
}
return high;
}

int findrow(long long n) {
int low, high, mid;
low
= 0;
high
= MAXN - 1;
while (low <= high) {
mid
= (low + high) / 2;
if (array[mid] == n) {
return mid;
}
else if (array[mid] >= n) {
high
= mid - 1;
}
else {
low
= mid + 1;
}
}
if (array[high] <= n) {
return high + 1;
}
return high;
}

void work() {
int T, i, j, k;
long long n;
init();
scanf(
"%d", &T);
while (T--) {
scanf(
"%I64d", &n);
i
= findlayer(n);
if (i > 1) {
n
= n - sum[i - 1];
}
j
= findrow(n);
k
= n - array[j - 1];
printf(
"%d %d %d\n", i + 1, j + 1, k);
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

你可能感兴趣的:(二分查找)