首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 云计算 >

答题报告 codeforces 242B

2012-11-23 
解题报告 codeforces 242B对于题目我不在此翻译,只给出链接。原题目链接如下:http://codeforces.com/proble

解题报告 codeforces 242B

对于题目我不在此翻译,只给出链接。

原题目链接如下:

http://codeforces.com/problemset/problem/242/B

解这小道题目让我大费周折。主要原因是轻视了。人一轻敌,思维就不缜密。自信满满,从而易有忽略之处,还不容易查出。

但从这道题目,我也看出自己的确有算法的潜质:我的解是最优的,所用内存最少——别人的答案中大都使用了数据结构存储输入的端点值,我没有存储。

下面给出源程序:

#include <stdio.h>

int main()
{
 int n, a, b, max = 0, min = 1024 * 1024 * 1024, indexMax = 0, indexMin = 0, index = 0;
 
 scanf("%d", &n);
 
 while(n > 0){
  scanf("%d%d", &a, &b);
  n--;
  index++;
  
  if(a < min){
   min = a;
   indexMin = index;
  }else if(a == min && b > max){
   max = b;
   indexMin = indexMax = index;
   continue;
  }
  
  if(b > max){
   max = b;
   indexMax = index;
  }else if(b == max && a < min){
   indexMax = index;
  }
  //起初就是忽视了这个判断,导致总是不能AC!
  if(a == min && b == max && indexMin != indexMax)
  {
   indexMin = indexMax = index;
  }
 }
 
 if(indexMax == indexMin)printf("%d\n", indexMin);
 else printf("-1\n");
 
 return 0;
}

 

 

1楼liyinchen8087小时前
不好意思,这个算法不易理解且有点罗嗦!codeforces上有几个更好的大家可以参考。

热点排行