1. 程式人生 > 實用技巧 >第十三場訓練賽

第十三場訓練賽

A - Heist

CodeForces - 1041A

昨天 ljm 團隊遭到了很嚴重的盜竊,萬惡的小偷將同學們夢寐以求的簽到題偷走了。

已知簽到題是從 x 按升序編號的。例如,如果 x = 4,並且有 3 個簽到題,那麼編號就為 4, 5, 6;如果 x = 10,並且有 7 個簽到題,那麼編號就為 10, 11, 12, 13, 14, 15, 16。

在盜竊之後,只有 n 個簽到題仍然存在,他們的編號分別為 a1, a2,…,an 。現在命題組要統計有多少道試題失竊,從而重新給同學們製作可愛的簽到題。由於命題人很懶,想要出盡可能少的題目,所以想求出最少有多少道簽到題失竊。命題人不記得 x ,也不記得原來有幾個簽到題。

PS:這是一道沒有被偷走的簽到題奧

Input

第一行輸入一個整數 n (1≤n≤1000),為仍然存在的簽到題數量。

第二行輸入 n 個整數 a_1,a_2 ,…, a_n (\(1≤a_i≤10^9\)),表示所有仍然存在的簽到題的編號。

Output

輸出被盜的簽到題的最小可能數量。

輸入1

4
10 13 12 8

輸出1

2

輸入2

5
7 5 6 4 8

輸出2

0

Note

在第一個樣例中,如果 x = 8 那麼最小的失竊題數是 2。 編號為 9 和 11 的題在盜竊中被偷了。

在第二個樣例中,如果 x = 4 則沒有題被偷

簽到題,AC程式碼

ll n;
void solve() {
    cin >> n;
    ll a[n + 1];
    for (int i = 1; i <= n; ++i) cin >> a[i];
    sort(a + 1, a + 1 + n);
    ll tmp = a[1];
    int cnt = 0;
    for (int i = 2; i <= n; ++i) {
        if (a[i] == tmp + 1)
            tmp = a[i];
        else {
            cnt += a[i] - tmp - 1;
            tmp = a[i];
        }
    }
    cout << cnt << endl;
}

B - Quasi Binary

CodeForces - 538B

如果一個數字的十進位制表示只包含數字0或1,則稱為準二進位制數。例如,數字0、1、101、110011 -是準二進位制數,而數字2、12、900則不是。 給定一個正整數n,將它表示為最小準二進位制數的和。

Input

輸入僅包含一個整數 \(n (1 ≤ *n* ≤ 106)\).

Output

第一行一個整數 k —用準二進位制數之和表示數n的最小數目

第二行輸出k 個元素。 這些元素之和等於 n.

不能輸出前導為0的數

如果存在多種方案輸出任意一種合法情況即可

Examples

Input

9

Output

9
1 1 1 1 1 1 1 1 1 

Input

32

Output

3
10 11 11 

AC程式碼

ll _, n, m, k;
int Size, tmp = 1;
int a[10];
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    while (n) {
        m = n % 10;
        for (int i = 9; i > 9 - m; --i) a[i] += tmp;
        Size = max(Size, (int)m);
        tmp *= 10, n /= 10;
    }
    cout << Size << endl;
    for (int i = 0; i < 10; ++i)
        if (a[i]) cout << a[i] << " ";
}

C - Multiply by 2, divide by 6

CodeForces - 1374B

給你一個整數 n. 每次必須執行二選一操作 ::

one: n=n*2

two: n=n/6 當且僅當 n%6==0

你的任務是找到最少操作次數 將n變成1 或者不存在這樣的操作方法.

你需要回答 t 組測試樣例

Input

第一行包含一個整數 t ( 1 <= t <= 2e4 )— 測試樣例數. 截下來包含 t 組樣例.

每組樣例僅包含一個整數 n ( 1 <= n <= 1e9 )

Output

對於每組測試樣例, 輸出一個整數 — 最小運算元使得 n 變成 1 若不存在則輸出-1

Example

Input

7
1
2
3
12
12345
15116544
387420489

Output

0
-1
2
-1
-1
12
36

AC程式碼

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long a, n, k, t;
    cin >> t;
    while (t--) {
        cin >> a;
        k = 0, n = 0;
        while (a % 6 == 0) { a /= 6; ++k; }
        while (a % 3 == 0) { a /= 3; k+=2; }
        if (a != 1)k = -1;
        cout << k << endl;
    }
}

D - Access System

ZOJ - 3787

For security issues, Marjar University has an access control system for each dormitory building.The system requires the students to use their personal identification cards to open the gate if they want to enter the building.

The gate will then remain unlocked for L seconds. For example L = 15, if a student came to the dormitory at 17:00:00 (in the format of HH:MM:SS) and used his card to open the gate. Any other students who come to the dormitory between [17:00:00, 17:00:15) can enter the building without authentication. If there is another student comes to the dorm at 17:00:15 or later, he must take out his card to unlock the gate again.

There are N students need to enter the dormitory. You are given the time they come to the gate. These lazy students will not use their cards unless necessary. Please find out the students who need to do so.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N (1 <= N <= 20000) and L (1 <= L <= 3600). The next N lines, each line is a unique time between [00:00:00, 24:00:00) on the same day.

Output

For each test case, output two lines. The first line is the number of students who need to use the card to open the gate. The second line the the index (1-based) of these students in ascending order, separated by a space.

Sample Input

3
2 1
12:30:00
12:30:01
5 15
17:00:00
17:00:15
17:00:06
17:01:00
17:00:14
3 5
12:00:09
12:00:05
12:00:00

Sample Output

2
1 2
3
1 2 4
2
2 3

沒想到 ZOJ竟然會卡輸出格式

// Author : RioTian
// Time : 20/10/20
#include <bits/stdc++.h>
#define ms(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
ll _, n, m, k, L;
vector<int> a;
struct node {
    int h, m, s, idx;
    // node(int hh, int mm, int ss, int ii) : h(hh), m(mm), s(ss), idx(ii){};
};
bool cmp(node a, node b) {
    return (a.h * 3600 + a.m * 60 + a.s) < (b.h * 3600 + b.m * 60 + b.s);
}
// void Print() {
//     for (int i = 1; i <= n; ++i)
//         printf("%2d:%2d:%2d %d\n", e[i].h, e[i].m, e[i].s, e[i].idx);
//     cout << endl;
// }
void solve() {
    a.clear();
    cin >> n >> L;
    node e[n + 1];
    for (int i = 1; i <= n; ++i) {
        int h, m, s;
        scanf("%2d:%2d:%2d", &h, &m, &s);
        // scanf("%2d:%2d:%2d", &e[i].h, &e[i].m, &e[i].s);
        e[i].h = h, e[i].m = m, e[i].s = s, e[i].idx = i;
    }
    // scanf("%2d:%2d:%2d", &e[i].h, &e[i].m, &e[i].s), e[i].idx = i;
    // Print();
    sort(e + 1, e + 1 + n, cmp);
    // Print();
    a.push_back(e[1].idx);
    node tmp = e[1];
    tmp.s += L;
    tmp.m += tmp.s / 60, tmp.s %= 60;
    tmp.h += tmp.m / 60, tmp.m %= 60;
    for (int i = 2; i <= n; ++i) {
        while (cmp(e[i], tmp)) ++i;
        if (i > n || e[i].h * 3600 + e[i].m * 60 + e[i].s >= 24 * 3600) break;
        a.push_back(e[i].idx);
        // printf("%d: %dh:%dm:%ds\n", i, e[i].h, e[i].m, e[i].s);
        tmp = e[i];
        tmp.s += L;
        tmp.m += tmp.s / 60, tmp.s %= 60;
        tmp.h += tmp.m / 60, tmp.m %= 60;
    }
    sort(a.begin(), a.end());
    cout << a.size() << endl;
    // for (auto p : a) cout << p << " ";
    for (int i = 0; i < a.size() - 1; ++i) cout << a[i] << " ";
    cout << a[a.size() - 1] << endl;
}
int main() {
    // freopen("in.txt", "r", stdin);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> _;
    while (_--) solve();
}

E - What day is that day?

ZOJ - 3785

回答,如果明天的明天是星期一,那麼昨天是星期幾?星期五??奧今天星期六??
如果今天是星期六,那麼我好奇H(N)=11 + 22 + 33 + ... + NN天之後是星期幾 ?不妨猜一猜。

Input

多組輸入 一個整數 T 代表樣例數.

每行輸入一個 N (1 <= N <= 1000000000).

Output

對於每一個樣例,輸出在H(N)天數後是星期幾?用英文表示

Sample Input

2
1
2

Sample Output

Sunday
Thursday

Hint

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.

思路:https://www.cnblogs.com/Simon-X/p/5110328.html

#include<cstdio>
int day[294] = {
    0, 1, 5, 4, 1, 4, 5, 5, 6, 0, 4, 6, 0, 6, 6, 0, 2, 0, 1, 6,
    0, 0, 1, 5, 6, 3, 0, 6, 6, 0, 1, 4, 6, 5, 6, 6, 0, 2, 4, 5,
    0, 6, 6, 0, 4, 3, 0, 3, 4, 4, 5, 6, 3, 5, 6, 5, 5, 6, 1, 6,
    0, 5, 6, 6, 0, 4, 5, 2, 6, 5, 5, 6, 0, 3, 5, 4, 5, 5, 6, 1,
    3, 4, 6, 5, 5, 6, 3, 2, 6, 2, 3, 3, 4, 5, 2, 4, 5, 4, 4, 5,
    0, 5, 6, 4, 5, 5, 6, 3, 4, 1, 5, 4, 4, 5, 6, 2, 4, 3, 4, 4,
    5, 0, 2, 3, 5, 4, 4, 5, 2, 1, 5, 1, 2, 2, 3, 4, 1, 3, 4, 3,
    3, 4, 6, 4, 5, 3, 4, 4, 5, 2, 3, 0, 4, 3, 3, 4, 5, 1, 3, 2,
    3, 3, 4, 6, 1, 2, 4, 3, 3, 4, 1, 0, 4, 0, 1, 1, 2, 3, 0, 2,
    3, 2, 2, 3, 5, 3, 4, 2, 3, 3, 4, 1, 2, 6, 3, 2, 2, 3, 4, 0,
    2, 1, 2, 2, 3, 5, 0, 1, 3, 2, 2, 3, 0, 6, 3, 6, 0, 0, 1, 2,
    6, 1, 2, 1, 1, 2, 4, 2, 3, 1, 2, 2, 3, 0, 1, 5, 2, 1, 1, 2,
    3, 6, 1, 0, 1, 1, 2, 4, 6, 0, 2, 1, 1, 2, 6, 5, 2, 5, 6, 6,
    0, 1, 5, 0, 1, 0, 0, 1, 3, 1, 2, 0, 1, 1, 2, 6, 0, 4, 1, 0,
    0, 1, 2, 5, 0, 6, 0, 0, 1, 3, 5, 6, 1, 0
};
char opt[7][10] = {
    "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
};
int main(){
    int t, a;
    scanf("%d", &t);
    while (t--){
        scanf("%d", &a);
        puts(opt[day[a % 294]]);
    }
    return 0;
}