只是记录
方案一(一个段错误)
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e5+10;
int n, p[N], cnt[N];
int main()
{
scanf("%d", &n);
int maxday = 0;
for(int i = 1; i <= n; i ++)
{
scanf("%d", &p[i]);
if(p[i]) cnt[p[i]-1] += 1;
maxday = max(maxday, p[i]);
}
int countN = 0;
for(int i = maxday; i > 0; i --)
{
countN += cnt[i];
//printf("countN is %d day is %d\n", countN, i);
if(countN >= i)
{
printf("%d\n", i);
return 0;
}
}
printf("0\n");
return 0;
}
方案而 (两个运行超时)
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5+10;
int n, p[N], cnt[N];
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++)
{
scanf("%d", &p[i]);
}
sort(p+1, p + n + 1);
p[0] = p[1]-1;
int j;
for(int i = n; i >= 0; i = j)
{
if(p[i])
{
j = i-1;
while(p[j] == p[i])
j--;
cnt[j] = i-j+cnt[i];
//printf("i is %d j is %d\n", i, j);
//printf("p[i] is %d p[j] is %d cnt[i] is %d\n", p[i], p[j], cnt[i]);
}
} // small to big
int countN = 0;
for(int i = n; i >= 0; i = j)
{
j = i-1;
while(p[j] == p[i])
j--;
int res = p[j+1];
while(res != p[j])
{
//printf("count is %d day is %d\n", cnt[i], res);
if(cnt[i] >= res)
{
printf("%d\n", res);
return 0;
}
res -- ;
}
}
printf("0\n");
return 0;
}