2019 年百度之星·程序设计大赛

1001

#pragma comment(linker, "/STACK:36777216")

#include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<int(n);++i)
#define FOR(i,a,b) for(int i=int(a);i<int(b);++i)
#define DWN(i,b,a) for(int i=int(b);i>=int(a);--i)

/// type define
typedef double DB;
typedef long long LL;
typedef std::vector<int>VI;
typedef std::vector<LL>VLL;
typedef std::pair<int, int>PII;
typedef std::pair<LL, LL>PLL;
typedef std::vector<PII >VPII;

/// const
static const double eps = 1e-8;
static const double pi = acos(-1.0);
static const int inf = 0x3f3f3f3f;
static const LL INF = 0x3f3f3f3f3f3f3f3f;

/// common statement
#define PB push_back
#define MP std::make_pair
#define fi first
#define se second

/****************************************************************/

const int maxn = 2e6 + 7;
const LL MOD = 1e9 + 7;

int a[maxn],b[maxn];

void solve() {
  /*********** start your code here. ************/
  int n;
  std::cin >> n;
  REP(i,n) std::cin >> a[i];
  REP(i,n) std::cin >>b[i];
  for(int i = n-1; i >= 0; --i) {
    int x = a[i],y = b[i];
    if(a[i] || b[i]) {
      if(!x)
        puts("0/1");
      else if(!y)
        puts("1/0");
      else {
        int z = std::__gcd(x,y);
        x /= z,y /= z;
        std::cout << x << "/" << y << "
";
      }
      break;
    }
  }
}

void init() {

}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  init();
  int T;
  std::cin >>T;
  while(T--)
    solve();
  return 0;
}

1002 Game

将每次移动都考虑成从区间到区间的变换。

不妨设当前所在的区间为 [l,r] ,下一个要到的区间为 [a,b]。那么,有以下 3 种情况

  1. [l,r] 和 [a,b] 有交集,那么取交集即可(因为起点是可选的),步数为 0
  2. [l,r] 在 [a,b] 的左侧,那么最小的步数就是从 a 走到 r ,由于一次可选走一步或者两步,最后一步的奇偶就影响的落点,可一步可两步就结果就是一个长为 2 的新区间(这个新区间必须在 [a,b] 内部,这里要判断)
  3. [l,r] 在 [a,b] 的右侧,类似情况 2,换成了从 b 走到 r
#include<bits/stdc++.h>

using namespace std;

const int inf = 0x3f3f3f3f;

void init() {

}

int n, l, r;

int transfer(int a, int b) {
  int res(0);
  int _l = max(l, a), _r = min(r, b);
  if(_l <= _r) {
    l = _l, r = _r;
    return 0;
  }
  if(b < l) {
    /// [a,b] [l,r]
    int res = (l - b + 1) / 2;
    r = b;
    l = b - ((l - b & 1) && a < b ? 1 : 0);
    return res;
  } else {
    /// [l,r] [a,b]
    res = (a - r + 1) / 2;
    l = a;
    r = a + (((a - r & 1) && b > a) ? 1 :0);
    return res;
  }
}

void solve() {
  int ans = 0;
  l = 1, r = inf;
  std::cin >> n;
  for(int i = 0, a, b; i < n; ++i) {
    std::cin >> a >> b;
    ans += transfer(a, b);
  }
  std::cout << ans << "
";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while(T--)
    solve();
  return 0;
}

1005

#pragma comment(linker, "/STACK:36777216")

#include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<int(n);++i)
#define FOR(i,a,b) for(int i=int(a);i<int(b);++i)
#define DWN(i,b,a) for(int i=int(b);i>=int(a);--i)

/// type define
typedef double DB;
typedef long long LL;
typedef std::vector<int>VI;
typedef std::vector<LL>VLL;
typedef std::pair<int, int>PII;
typedef std::pair<LL, LL>PLL;
typedef std::vector<PII >VPII;

/// const
static const double eps = 1e-8;
static const double pi = acos(-1.0);
static const int inf = 0x3f3f3f3f;
static const LL INF = 0x3f3f3f3f3f3f3f3f;

/// common statement
#define PB push_back
#define MP std::make_pair
#define fi first
#define se second

/****************************************************************/

const int maxn = 2e6 + 7;
const LL MOD = 1e9 + 7;

LL C(LL x){
  LL step[] = {3,4,3,1,6,1};
  LL b[] = {0,1,1,0,3,0};

    return b[x%6]+x/6*step[x%6];
}

void solve() {
  /*********** start your code here. ************/
  LL n;std::cin >>n;
  std::cout << C(n) << "
";
}



void init() {

}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  //init();
  int T;
  std::cin >>T;
  while(T--)
    solve();
  return 0;
}
原文地址:https://www.cnblogs.com/Forgenvueory/p/11374092.html