CF1293A - ConneR and the A.R.C. Markland-N 二分

Posted iat14

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
");
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
",ans);
42     }
43     return 0;
44 }

 

以上是关于CF1293A - ConneR and the A.R.C. Markland-N 二分的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #614 (Div. 2) A( A - ConneR and the A.R.C. Markland-N)

Codeforces Round #614 (Div. 2)

CF 862C Mahmoud and Ehab and the xor(异或)

[CF842C]Ilya And The Tree

CF438D The Child and Sequence

CF 842C Ilya And The Tree(树上DFS)