Untitled
C
views 33
,
size 2642 b
``````12220
#include<stdio.h>
long long int num[1000005];
long long int sort1(long long int a, long long int b, long long int tar)
{
long long int m=0;
long long int min = 1000000000000000;
long long int m_min = 0;
if (num[b] < tar) return 0; //如果超過最大的數字則return 0
if (num[a] > tar) {
return a;
}
while (a <= b) { //二分搜尋法 找到小的就比較
m = (a + b )/ 2;
if (tar > num[m]) {
a = m + 1;
}
else if (tar < num[m]) {
if (num[m] - tar < min) {
min = num[m] - tar;
m_min = m;
}
b = m - 1;
}
else {
m_min = m;
break;
}
}
return m_min;
}
int main()
{
long long int n, k, q;
long long int f;
long long int p1, p2;
long long int i;
scanf("%lld%lld%lld", &n, &k, &q);
for (i = 1; i <= n; i++) {
scanf("%lld", &num[i]);
}
for (i = 1; i <= q; i++) {
scanf("%lld", &f);
if (f > num[n]) { //如果超過最大 則不能比 直接Print
printf("gan ni nya sai bau\n");
continue;
}
p1 = sort1(1, k, f);
p2 = sort1(k + 1, n, f);
if (p1 == 0) { //如果在1~k找不到則一定在p2
printf("%lld\n", p2 - k);
continue;
}
if((num[p1]-f)<(num[p2]-f)) printf("%lld\n", p1 + n - k);
else printf("%lld\n", p2 - k); //看誰比較小 就print出來

}

return 0;
}
12220
#include<stdio.h>
long long int num[1000005];
long long int sort1(long long int a, long long int b, long long int tar)
{
long long int m=0;
long long int min = 1000000000000000;
long long int m_min = 0;
if (num[b] < tar) return 0; //如果超過最大的數字則return 0
if (num[a] > tar) {
return a;
}
while (a <= b) { //二分搜尋法 找到小的就比較
m = (a + b )/ 2;
if (tar > num[m]) {
a = m + 1;
}
else if (tar < num[m]) {
if (num[m] - tar < min) {
min = num[m] - tar;
m_min = m;
}
b = m - 1;
}
else {
m_min = m;
break;
}
}
return m_min;
}
int main()
{
long long int n, k, q;
long long int f;
long long int p1, p2;
long long int i;
scanf("%lld%lld%lld", &n, &k, &q);
for (i = 1; i <= n; i++) {
scanf("%lld", &num[i]);
}
for (i = 1; i <= q; i++) {
scanf("%lld", &f);
if (f > num[n]) { //如果超過最大 則不能比 直接Print
printf("gan ni nya sai bau\n");
continue;
}
p1 = sort1(1, k, f);
p2 = sort1(k + 1, n, f);
if (p1 == 0) { //如果在1~k找不到則一定在p2
printf("%lld\n", p2 - k);
continue;
}
if((num[p1]-f)<(num[p2]-f)) printf("%lld\n", p1 + n - k);
else printf("%lld\n", p2 - k); //看誰比較小 就print出來

}

return 0;
}``````