hihoCoder 1399 Shortening Sequence

介绍2017微软秋季校园招聘在线编程笔试A,提到根据N的范围可判断算法复杂度,采用stack存储数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2017微软秋季校园招聘在线编程笔试A
看N的范围就知道是用什么复杂度的算法了
用个stack存就好了

  #define LOG(x) cout << #x << " = " << (x) << endl
  #define PRINTLN(x) cout << (x) << endl
  #define MEM(x, y) memset((x), (y), sizeof((x)))
  #include <bits/stdc++.h>
  using namespace std;
  const double PI = 2*acos(0);
  typedef long long ll;
  typedef complex<double> Complex;
  int nextInt()
  {
    int x;
    scanf("%d", &x);
    return x;
  }
  ll nextLL()
  {
    ll x;
    scanf("%lld", &x);
    return x;
  }
  //TEMPLATE

  //MAIN
  int main()
  {
    //freopen("in.txt", "r", stdin);
    int n;
    while (scanf("%d", &n) != EOF) {
         stack<ll> s;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      ll t = nextLL();
      if (!s.empty() && (s.top() + t) % 2) {
        s.pop();
        ans += 2;
      } else s.push(t);
    }
    PRINTLN(n - ans);
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值