关于杭电acm1025解决方法
关于杭电acm1025
这是我此题AC的代码,此题在这种时候能AC
但是当我把月亮行全局变量定义到main函数内部(即星星行),再提交时就会有RuntimeError的错误。
真郁闷了。。难道这个a[500001]一定要定义为全局变量吗。
此题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025
#include <stdio.h>
#include <stdlib.h>
struct city
{
int poor, rich;
}cities[500001];
int cmp(const void* a, const void* b)
{
return (*(city*)a).rich > (*(city*)b).rich ? 1 : -1;
}
int a[500001];//月亮行
int main()
{
int N;
int i, j;
//int a[500001];////////////////////星星行
int roads;
int s, e, m;
int count = 0;
while(scanf("%d", &N) != EOF)
{
count ++;
for (i = 0; i < N; i++) scanf("%d%d", &cities[i].poor, &cities[i].rich);
qsort(cities, N, sizeof(cities[0]), cmp);
for (i = 0; i < N; i++) a[i] = 0;
a[0] = -1;
roads = 1;
a[1] = cities[0].poor;
for (i = 1; i < N; i++)
{
s = 0;
e = roads;
while (s <= e)
{
m = (s + e) / 2;
if (a[m] <= cities[i].poor) s = m + 1;
else e = m - 1;
}
a[s] = cities[i].poor;
if (s > roads) roads++;
}
printf("Case %d:\n",count);
if(roads == 1) printf("My king, at most %d road can be built.\n\n", roads);
else printf("My king, at most %d roads can be built.\n\n", roads);
}
return 0;
}
------解决方案--------------------
定义在main函数里,这个变量就在main函数的栈空间里,有可能不够用的。放在全局变量里,这个数据就在全局区里。
这是我此题AC的代码,此题在这种时候能AC
但是当我把月亮行全局变量定义到main函数内部(即星星行),再提交时就会有RuntimeError的错误。
真郁闷了。。难道这个a[500001]一定要定义为全局变量吗。
此题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025
#include <stdio.h>
#include <stdlib.h>
struct city
{
int poor, rich;
}cities[500001];
int cmp(const void* a, const void* b)
{
return (*(city*)a).rich > (*(city*)b).rich ? 1 : -1;
}
int a[500001];//月亮行
int main()
{
int N;
int i, j;
//int a[500001];////////////////////星星行
int roads;
int s, e, m;
int count = 0;
while(scanf("%d", &N) != EOF)
{
count ++;
for (i = 0; i < N; i++) scanf("%d%d", &cities[i].poor, &cities[i].rich);
qsort(cities, N, sizeof(cities[0]), cmp);
for (i = 0; i < N; i++) a[i] = 0;
a[0] = -1;
roads = 1;
a[1] = cities[0].poor;
for (i = 1; i < N; i++)
{
s = 0;
e = roads;
while (s <= e)
{
m = (s + e) / 2;
if (a[m] <= cities[i].poor) s = m + 1;
else e = m - 1;
}
a[s] = cities[i].poor;
if (s > roads) roads++;
}
printf("Case %d:\n",count);
if(roads == 1) printf("My king, at most %d road can be built.\n\n", roads);
else printf("My king, at most %d roads can be built.\n\n", roads);
}
return 0;
}
------解决方案--------------------
定义在main函数里,这个变量就在main函数的栈空间里,有可能不够用的。放在全局变量里,这个数据就在全局区里。