Canadian Coding Competition(CCC) 2020 Senior 题解

Problem S1: Surmising a Sprinter’s Speed 

Problem S1: 推测短跑运动员的速度 

Problem Description

Trick E. Dingo is trying, as usual, to catch his nemesis the Street Sprinter. His past attempts using magnets, traps and explosives have failed miserably, so he’s catching his breath to gather observational data and learn more about how fast Street Sprinter is. Trick E. Dingo and Street Sprinter both inhabit a single straight west-east road with a particularly famous rock on it known affectionately as The Origin. Positions on this straight road are measured numerically according to the distance from The Origin, and using negative numbers for positions west of The Origin and positive numbers for positions east of The Origin. The observations by Trick E. Dingo each contain two numbers: a time, and the value of Street Sprinter’s position on the road at that time. Given this information, what speed must Street Sprinter must be capable of?

Input Specification

The first line contains a number 2 ≤ N ≤ 100 000, the number of observations that follow. The next N lines each contain an integer 0 ≤ T ≤ 1 000 000 000 indicating the time, in seconds, of when a measurement was made, and an integer −1 000 000 000 ≤ X ≤ 1 000 000 000 indicating the position, in metres, of the Street Sprinter at that time. No two lines will have the same value of T. For 7 of the 15 available marks, N ≤ 1000.

Output Specification

Output a single number X, such that we can conclude that Street Sprinter’s speed was at least X metres/second at some point in time, and such that X is as large as possible. If the correct answer is C, the grader will view X as correct if |X − C|/C < 10−5 . 

Sample Input 1

3

0 100

20 50

10 120

Output for Sample Input 1

7.0

Explanation of Output for Sample Input 1

Since the Street Sprinter ran from position 100 to position 120 between time 0 and time 10, we know its speed must have been at least 2 at some point in time: if it was always less than 2, then the distance of 20 could not be covered in 10 seconds. Likewise, the speed must have been at least 7 in order to travel between position 120 and 50 in 10 seconds.

Problem J5/S2: Escape Room

Problem Description

You have to determine if it is possible to escape from a room. The room is an M-by-N grid with each position (cell) containing a positive integer. The rows are numbered 1, 2, . . . , M and the columns are numbered 1, 2, . . . , N. We use (r, c) to refer to the cell in row r and column c. You start in the top-left corner at (1, 1) and exit from the bottom-right corner at (M, N). If you are in a cell containing the value x, then you can jump to any cell (a, b) satisfying a × b = x. For example, if you are in a cell containing a 6, you can jump to cell (2, 3). Note that from a cell containing a 6, there are up to four cells you can jump to: (2, 3), (3, 2), (1, 6), or (6, 1). If the room is a 5-by-6 grid, there isn’t a row 6 so only the first three jumps would be possible.

Input Specification

The first line of the input will be an integer M (1 ≤ M ≤ 1000). The second line of the input will be an integer N (1 ≤ N ≤ 1000). The remaining input gives the positive integers in the cells of the room with M rows and N columns. It consists of M lines where each line contains N positive integers, each less than or equal to 1 000 000, separated by single spaces. For 1 of the 15 available marks, M = 2 and N = 2. For an additional 2 of the 15 available marks, M = 1. For an additional 4 of the 15 available marks, all of the integers in the cells will be unique. For an additional 4 of the 15 available marks, M ≤ 200 and N ≤ 200.

Output Specification

Output yes if it is possible to escape from the room. Otherwise, output no.

Problem S3: Searching for Strings

Problem Description

You’re given a string N, called the needle, and a string H, called the haystack, both of which contain only lowercase letters “a”..“z”. Write a program to count the number of distinct permutations of N which appear as a substring of H at least once. Note that N can have anywhere between 1 and |N|! distinct permutations in total – for example, the string “aab” has 3 distinct permutations (“aab”, “aba”, and “baa”).

Input Specification

The first line contains N (1 ≤ |N| ≤ 200 000), the needle string. The second line contains H (1 ≤ |H| ≤ 200 000), the haystack string. For 3 of the 15 available marks, |N| ≤ 8 and |H| ≤ 200. For an additional 2 of the 15 available marks, |N| ≤ 200 and |H| ≤ 200. For an additional 2 of the 15 available marks, |N| ≤ 2000 and |H| ≤ 2000.

Output Specification

Output consists of one integer, the number of distinct permutations of N which appear as a substring of H.

Sample Input

aab

abacabaa

Output

for Sample Input 2 Explanation of Output for Sample Input The permutations “aba” and “baa” each appear as substrings of H (the former appears twice), while the permutation “aab” does not appear.

#include <cstdio>
#include <cstring>

#include <string>
#include <unordered_set>

#define MAX_N 200000
#define MAX_H 200000
#define NUM_ALPHABET 26

int  n, h;
char needle  [MAX_N + 1];
char haystack[MAX_H + 1];
int  hist[NUM_ALPHABET] = { 0 };

void read_input() {
	fgets(needle, sizeof(needle), stdin);
	n = strlen(needle);
	if (needle[n - 1] == '\n') {
		needle[n - 1] == '\0';
		n--;
	}

	fgets(haystack, sizeof(haystack), stdin);
	h = strlen(haystack);
	if (haystack[h - 1] == '\n') {
		haystack[h - 1] == '\0';
		h--;
	}
}

void compute_hist() {
	for (int i = 0; i < n; i++)
		hist[needle[i] - 'a']++;
}

class pre_hashed_string_view {
public:
	pre_hashed_string_view(const char *str, int n, size_t hash) : str(str), n(n), hash(hash) {}
	bool operator ==(const pre_hashed_string_view &b) const {
		if (n != b.n)
			return false;
		return strncmp(str, b.str, n) == 0;
	}

	const char *str;
	int n;
	size_t hash;
};

namespace std {
	template<> struct hash<pre_hashed_string_view> {
		size_t operator()(pre_hashed_string_view const& obj) const noexcept {
			return obj.hash;
		}
	};
}

/*
 * We compute a histogram of each of the possible substring positions
 * and compare with the original histogram of the needle string.
 * For each match we add the substring to a hashset and then return
 * the number of unique values.
 */
int solve() {
	int running_hist[NUM_ALPHABET] = { 0 };

	const int prime = 2000003;
	int power = 1;
	int hash = 0;

	for (int i = 0; i < n - 1; i++)
		power = (power * NUM_ALPHABET) % prime;

	/* compute histogram for the first position */
	for (int i = 0; i < n; i++) {
		running_hist[haystack[i] - 'a']++;
		hash = (NUM_ALPHABET * hash + (haystack[i] - 'a' + 1)) % prime;
	}

	std::unordered_set<pre_hashed_string_view> permutations;

	/* check the first position against the original histogram */
	if (memcmp(hist, running_hist, sizeof(int) * NUM_ALPHABET) == 0)
		permutations.emplace(haystack, n, hash);

	/* iterate through the remaining positions */
	for (int i = 0; i < h - n; i++) {
		/* update the histogram */
		running_hist[haystack[i]     - 'a']--;
		running_hist[haystack[i + n] - 'a']++;

		/* update the hash */
		hash = (NUM_ALPHABET * (hash - ((haystack[i] - 'a' + 1) * power)) + (haystack[i + n] - 'a' + 1)) % prime;
		if (hash < 0)
			hash = hash + prime;

		/* check the position */
		if (memcmp(hist, running_hist, sizeof(int) * NUM_ALPHABET) == 0)
			permutations.emplace(&haystack[i + 1], n, hash);
	}

	return permutations.size();
}

int main() {
	read_input();
	compute_hist();
	int permutations = solve();
	printf("%d", permutations);
	return 0;
}

Problem S4: Swapping Seats

Problem Description

There are N people sitting at a circular table for a long session of negotiations. Each person belongs to one of the three groups: A, B, or C. A group is happy if all of its members are sitting contiguously in a block of consecutive seats. You would like to make all groups happy by some sequence of swap operations. In each swap operation, two people exchange seats with each other. What is the minimum number of swaps required to make all groups happy?

Input Specification

The input consists of a single line containing N (1 ≤ N ≤ 1 000 000) characters, where each character is A, B, or C. The i-th character denotes the group of the person initially sitting at the i-th seat at the table, where seats are numbered in clockwise order. For 4 of the 15 available marks, the input has no C characters and N ≤ 5 000. For an additional 4 of the 15 available marks, the input has no C characters. For an additional 4 of the 15 available marks, N ≤ 5 000.

Output Specification

Output a single integer, the minimum possible number of swaps.

#include <climits>
#include <cstdio>
#include <cstring>

#include <algorithm>

#define MAX_N 1000000

int n;
char data[MAX_N + 2];

static void read_input() {
	fgets(data, sizeof(data), stdin);
	n = strlen(data);
        if (data[n - 1] == '\n') {
                data[n - 1] == '\0';
                n--;
        }
}

struct counts {
	int a = 0;
	int b = 0;
	int c = 0;
};

#define INC(d, ch) switch((ch)) { case 'A': (d).a++; break; case 'B': (d).b++; break; case 'C': (d).c++; break; default: exit(1); }
#define DEC(d, ch) switch((ch)) { case 'A': (d).a--; break; case 'B': (d).b--; break; case 'C': (d).c--; break; default: exit(1); }

static counts count_range(int start, int end) {
	counts c;

	for (int i = start; i < end; i++)
		INC(c, data[i])

	return c;
}

Problem S5: Josh’s Double Bacon Deluxe

Problem Description

On their way to the contest grounds, Josh, his coach, and his N − 2 teammates decide to stop at a burger joint that offers M distinct burger menu items. After ordering their favourite burgers, the team members line up, with the coach in the first position and Josh last, to pick up their burgers. Unfortunately, the coach forgot what he ordered. He picks a burger at random and walks away. The other team members, in sequence, pick up their favourite burger if available, or a random remaining burger if there are no more of their favourite burger. What is the probability that Josh, being last in line, will get to eat his favourite burger?

Input Specification

The first line contains the number N (3 ≤ N ≤ 1 000 000), the total number of people and burgers. The next line contains N numbers, the i-th being bi (1 ≤ bi ≤ M ≤ 500 000), denoting the item number of the i-th person’s favourite burger. The first person in line is the coach, and the N-th person is Josh. For 4 of the 15 available marks, N ≤ 100 000 and M ≤ 1000 For an additional 5 of the 15 available marks, M ≤ 5000.

Output Specification

Output a single number P, the probability that Josh will get to eat his favourite burger, bN . If the correct answer is C, the grader will view P correct if |P − C| < 10−6 .

#include <bits/stdc++.h>
using namespace std;   

const long long MAX=1e6+2;
long long N, M, K;
vector<long long> arr;


int main() {
    cin >> N >> M >> K;
    for (long long i=0; i<N; i++) {
        long long largest_possible=N-i-1, adding;
        long long adder=min(K-largest_possible, M);
        if (adder==0) break;
        if (adder>i) {
            adding=min(i+1, M);
            adder=adding;
        }
        else {
            adding=arr[i-adder];
        }
        arr.push_back(adding);
        K-=adder;
    }
    if (K==0 && arr.size()==N) {
        for (long long i=0; i<N; i++) {
            cout << arr[i];
            if (i != N - 1) cout << " ";
        }
    }
    else {
        cout << -1;
    }
    cout << "\n";
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值