CF1293A - ConneR and the A.R.C. Markland-N 二分
不能吃饭的楼层有限,分类讨论下就好。没注意lower_bound查不到会出界这个问题,卡了好久......
用map当数组做应该会更简单。
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 int T,n,s,k,ans,loc[1100]; 5 int main() 6 { 7 for (scanf("%d",&T);T;T--) 8 { 9 scanf("%d%d%d",&n,&s,&k); 10 k = min(n - 1,k); 11 for (int i = 1;i <= k;i++) 12 scanf("%d",&loc[i]); 13 sort(loc + 1,loc + k + 1); 14 int t = lower_bound(loc + 1,loc + k + 1,s) - loc; 15 if (t == k + 1) 16 t = k; 17 else if (t == 0) 18 t = 1; 19 if (loc[t] != s) 20 { 21 printf("0\n"); 22 continue; 23 } 24 ans = 1100000; 25 if (loc[1] != 1) 26 ans = min(ans,abs(s - (loc[1] - 1))); 27 if (loc[k] != n) 28 ans = min(ans,abs(s - (loc[k] + 1))); 29 for (int i = t + 1;i <= k;i++) 30 if (loc[i] != loc[i - 1] + 1) 31 { 32 ans = min(ans,(loc[i - 1] + 1) - s); 33 break; 34 } 35 for (int i = t - 1;i >= 1;i--) 36 if (loc[i] != loc[i + 1] - 1) 37 { 38 ans = min(ans,s - (loc[i + 1] - 1)); 39 break; 40 } 41 printf("%d\n",ans); 42 } 43 return 0; 44 }
心之所动 且就随缘去吧