前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Global Round 2 A. Ilya and a Colorful Walk(思维)

Codeforces Global Round 2 A. Ilya and a Colorful Walk(思维)

作者头像
Ch_Zaqdt
发布2019-04-18 10:09:39
3000
发布2019-04-18 10:09:39
举报
文章被收录于专栏:Zaqdt_ACM

版权声明:欢迎转载,若转载,请标明出处,如有错误,请指点,也欢迎大佬们给出优化方法 https://blog.csdn.net/Charles_Zaqdt/article/details/89066341

题目链接:https://codeforces.com/contest/1119/problem/A

       题意是输入n个数,然后找出两个不同的数使得他们之间的距离最大,输出这个最大的距离。

       我的思路就是记录当前数的最左端和最右端的位置,然后分情况讨论一下就好了。


AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
int n;
map<int,int> ma1,ma2;

int main()
{
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    int x;
    scanf("%d",&x);
    if(ma1[x] == 0) ma1[x] = i;
    ma2[x] = i;
  }
  int ans = 0;
  for(int i=1;i<=n;i++){
    if(ma1[i] == ma2[i] && ma1[i] == 0) continue;
    if(ma1[i] == 1 && ma2[i] == n) continue;
    if(ma1[i] != 1 && ma2[i] != n){
      ans = max(ans, max(ma2[i] - 1, n - ma1[i]));
      continue;
    }
    if(ma1[i] == 1 || ma2[i] == n){
      ans = max(ans, n - 1);
      continue;
    }
  }
  printf("%d\n", ans);
  return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年04月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档