昨天在codeforces上做题的时候,做到一道题把我WA到死(绝望),后来发现自己忘记考虑了一堆边界条件(我是真的菜),在这里重新回顾一下我的艰辛的做题过程。
题目:Codeforces-977C-Less or Equal
因为本人比较菜,而且题目也比较简单,我就直接放出AC代码然后在上面做注释说明了。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<list>
#include<cmath>
#include<stack>
using namespace std;
long long a[200001];//这里WA了两次,之前少了个0……
long long n, k, x;
long long current;
int main()
{
cin >> n >> k;
if (k > n)
{
cout << -1;
return 0;
}
n--;
for (long long i = 0; i <= n; i++)
{
cin >> a[i];
}
sort(a, a + n + 1);
if (a[0] > 1 && k == 0)//这里的边界判断WA了两次才改对……
{
cout << a[0] - 1;
return 0;
}
else if(k==0 && a[0]==1)
{
cout << -1;
return 0;
}
x = a[0];
while(current <= n)//这里的代码重写过,改了n次,这里全是边界判断,我就偷懒不说明了……
{
while (a[current] <= x)
{
current++;
if (current > n)break;
}
if (current == k)
break;
if (x > a[n] || current > k)
{
x = -1;
break;
}
x = a[current];
}
cout << x;
return 0;
}
发表评论