题目的大意便是找到 $|T - h_i \times 6 \times 10^{-3} - A|$ 为最小值的下标并输出 $i$。
因此我们先将这个值设为 0x3f3f3f3f,即正无穷,然后每次输入进行一次比较,若比原答案更优,则将其进行更新,并记录下标。最后输出下标即可,时间复杂度为 $O(n)$。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #define init(x) memset (x,0,sizeof (x)) #define ll long long #define ull unsigned long long #define INF 0x3f3f3f3f using namespace std; const int MAX = 1e5 + 5; const int MOD = 1e9 + 7; ll read (); int n,t,A,ans; double sum = INF; int main () { n = read ();t = read ();A = read (); for (int i = 1;i <= n;++i) { int x;x = read (); if (sum > abs (t - x * 0.006 - A)) ans = i,sum = abs (t - x * 0.006 - A); } printf ("%d\n",ans); return 0; } ll read () { ll s = 0;int f = 1; char ch = getchar (); while ((ch < '0' || ch > '9') && ch != EOF) { if (ch == '-') f = -1; ch = getchar (); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar (); } return s * f; }
|