问题模型
环形序列的最长非负连续子序列
<span style="font-size:18px;">#include <stdio.h>
#include<string.h>
int a[200010]; //存储生活费和花费之差
int ans[200010]; //ans[i] 存储的是以i结尾最优值
int main()
{
int n;
int i;
int w, l;
while ((scanf("%d", &n)) != EOF)
{
memset(a, 0, sizeof(a));
memset(ans, 0, sizeof(ans));
for (i = 0; i < n; i++)
{
scanf("%d%d", &w, &l);
a[i] = a[i + n] = w - l;
ans[i] = ans[i + n] = 1;
}
for (i = 1; i < 2 * n; i++)
{
if (a[i] + a[i - 1] >= 0 && a[i - 1] >= 0)
{
a[i] = a[i] + a[i - 1];
ans[i] += ans[i - 1];
if (ans[i] == n)
{
break;
}
}
}
int max1 = 0;
for (i = 0; i < 2 * n; i++)
{
if (max1 < ans[i])
{
max1 = ans[i];
}
}
printf("%d\n", max1);
}
return 0;
}
</span>
搜索
复制