Codeforces Round #786 (Div. 3)

简介: 笔记

A Number Transformation


10.png

样例#1

输入#1

3

3 75

100 100

42 13

输出#1

2 5

3 1

0 0

void solve() {
  int x, y; cin >> x >> y;
  if(y % x != 0) {
    cout << 0 << ' ' << 0 << endl;
  } else {
    cout << 1 << ' ' << y / x << endl;
  }
}

B Dictionary


题 意 : 规 则 : a b = 1 , a c = 2 , . . . , a z = 25 , b a = 26 , b c = 27 , . . . , z y = 650


思路:打表O(1)输出即可。见代码...


样例#1


输入#1

7

ab

ac

az

ba

bc

zx

zy


输出#1


1

2

25

26

27

649

650

map<string, int> mp;
void init() {
  int cnt = 1;
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < 26; j++) {
      if(i == j) continue;
      char a = i + 'a';
      char b = j + 'a';
      string x = "";
      x += a, x += b;
      mp[x] = cnt++;
    }
  }
}
void solve() {
  string s; cin >> s;
  cout << mp[s] << endl;
}
signed main() {
  IOS int _ = 1;
  init();
  cin >> _;
  while(_--) { solve(); }
  return 0;
}

C Infinite Replacement


题意:给定母串S(全由字符'a'组成),替换串T。现可以将S中一个字符'a'替换为整个T,问能变成多少种不同串,可以替换任意次。


输出最多能变成的不同串的个数,能变无限多则输出-1


思路:


发现这有三种情况:

11.png

样例#1

输入#1

3

aaaa

a

aa

abc

a

b

输出#1

1

-1

2

void solve() {
  string s; cin >> s;
  string t; cin >> t;
  int n = s.sz, m = t.sz;
  int res = -1;
  if(m == 1) {
    if(t == "a") res = 1;
    else res = (1ll << n);
  } else {
    if(t.find("a") != -1) res = -1;
    else res = (1ll << n);
  }
  cout << res << endl;
}

D A-B-C Sort


12.png

样例#1

输入#1

3

4

3 1 5 3

3

3 2 1

1

7331

输出#1

YES

NO

YES

int a[N];
void solve() {
  int n; cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = n - 1; i >= 1; i -= 2) {
    if(a[i] < a[i - 1]) {
      swap(a[i], a[i - 1]);
    }
  }
  bool o = true;
  for (int i = 0; i < n - 1; i++) {
    if(a[i] > a[i + 1]) o = false;
  }
  puts(o ? "YES" : "NO");
}

E Breaking the Wall


13.png

int a[N];
void solve()
{
  int n; cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int res = 0x3f3f3f3f;
  for (int i = 1; i < n; i++) {
    int x = a[i], y = a[i + 1];
    if(x < y) swap(x, y);
    int cnt = min(x - y, (x + 1) / 2);
    x -= 2 * cnt;
    y -= cnt;
    if(x > 0 && y > 0) cnt += (x + y - 1) / 3 + 1;
    res = min(res, cnt);
  }
  for (int i = 1; i < n - 1; i++) {
    int cnt = 0;
    int x = a[i], y = a[i + 2];
    if(x < y) swap(x, y);
    res = min(res, y + (x - y + 1) / 2);
  }
  sort(a + 1, a + 1 + n);
  res = min(res, (a[1] + 1) / 2 + (a[2] + 1) / 2);
  cout << res << endl;
}

F Desktop Rearrangement


G Remove Directed Edges


相关文章
|
6月前
|
人工智能 算法 BI
Codeforces Round #179 (Div. 2)A、B、C、D
我们每次加进来的点相当于k,首先需要进行一个双重循环找到k点和所有点之间的最短路径;然后就以k点位判断节点更新之前的k-1个点,时间复杂度降到O(n^3),而暴力解法每次都要进行floyd,时间复杂度为O(n^4);相比之下前述解法考虑到了floyd算法的性质,更好了运用了算法的内质。
31 0
|
11月前
Codeforces Round 799 (Div. 4)
Codeforces Round 799 (Div. 4)
91 0
Codeforces Round #644 (Div. 3)(A~G)
Codeforces Round #644 (Div. 3)(A~G)
95 0
Equidistant Vertices-树型dp-Codeforces Round #734 (Div. 3)
Description A tree is an undirected connected graph without cycles. You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words,
119 0
Codeforces Round #723 (Div. 2)B. I Hate 1111
Description You are given an integer x. Can you make x by summing up some number of 11,111,1111,11111,…? (You can use any number among them any number of times). For instance, 33=11+11+11 144=111+11+11+11
141 0
Codeforces Round #723 (Div. 2)B. I Hate 1111