【kickstart】2020 B round

比賽鏈接:https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc8

賽後總結

其實有很久沒有刷題了,從博客數量可以看出來,但這輪比賽還是去做了,所以4道題只能做出簡單的2道半來,水平就那樣了...

 

題目

由於時間關係,先只總結前3題了。

1.Bike Tour

Li has planned a bike tour through the mountains of Switzerland. His tour consists of N checkpoints, numbered from 1 to N in the order he will visit them. The i-th checkpoint has a height of Hi.

A checkpoint is a peak if:

  • It is not the 1st checkpoint or the N-th checkpoint, and
  • The height of the checkpoint is strictly greater than the checkpoint immediately before it and the checkpoint immediately after it.

Please help Li find out the number of peaks.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Hi.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of peaks in Li's bike tour.

Limits

Time limit: 10 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ Hi ≤ 100.

Test set 1

3 ≤ N ≤ 5.

Test set 2

3 ≤ N ≤ 100.

Sample

Input Output
4
3
10 20 14
4
7 7 7 7
5
10 90 20 90 10
3
10 3 10
Case #1: 1
Case #2: 0
Case #3: 2
Case #4: 0

  
  • In sample case #1, the 2nd checkpoint is a peak.
  • In sample case #2, there are no peaks.
  • In sample case #3, the 2nd and 4th checkpoint are peaks.
  • In sample case #4, there are no peaks.

題目鏈接:https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc8/00000000002d82e6

思路

這道題就是模擬即可。時間複雜度O(n),空間複雜度O(1)。

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;

void solve()    // 每個case裏的東西
{
    ll t;
    cin >> t;
    vector<ll> h(t);
    for (ll i = 0; i<t; ++i){
        cin>> h[i];
    }
    ll res = 0;
    for(ll i = 1; i<t-1; ++i){
        if(h[i]>h[i-1] && h[i]>h[i+1]){
            ++res;
        }
    }
    cout<<res;
}

int main(){
    int t = 0;
    cin >> t;
    for (int i = 1; i <= t; ++i)
    {
        cout << "Case #" << i << ": ";
        solve();
        if(i != t)
            cout<<endl;
    }
    return 0;
}

 

2.Bus Routes

Bucket is planning to make a very long journey across the countryside by bus. Her journey consists of N bus routes, numbered from 1 to N in the order she must take them. The buses themselves are very fast, but do not run often. The i-th bus route only runs every Xi days.

More specifically, she can only take the i-th bus on day Xi, 2Xi, 3Xi and so on. Since the buses are very fast, she can take multiple buses on the same day.

Bucket must finish her journey by day D, but she would like to start the journey as late as possible. What is the latest day she could take the first bus, and still finish her journey by day D?

It is guaranteed that it is possible for Bucket to finish her journey by day D.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case begins with a line containing the two integers N and D. Then, another line follows containing N integers, the i-th one is Xi.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the latest day she could take the first bus, and still finish her journey by day D.

Limits

Time limit: 10 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ Xi ≤ D.
1 ≤ N ≤ 1000.
It is guaranteed that it is possible for Bucket to finish her journey by day D.

Test set 1

1 ≤ D ≤ 100.

Test set 2

1 ≤ D ≤ 1012.

Sample

Input Output
3
3 10
3 7 2
4 100
11 10 5 50
1 1
1
Case #1: 6
Case #2: 99
Case #3: 1

  

In Sample Case #1, there are N = 3 bus routes and Bucket must arrive by day D = 10. She could:

  • Take the 1st bus on day 6 (X1 = 3),
  • Take the 2nd bus on day 7 (X2 = 7) and
  • Take the 3rd bus on day 8 (X3 = 2).

In Sample Case #2, there are N = 4 bus routes and Bucket must arrive by day D = 100. She could:

  • Take the 1st bus on day 99 (X1 = 11),
  • Take the 2nd bus on day 100 (X2 = 10),
  • Take the 3rd bus on day 100 (X3 = 5) and
  • Take the 4th bus on day 100 (X4 = 50),

In Sample Case #3, there is N = 1 bus route and Bucket must arrive by day D = 1. She could:

  • Take the 1st bus on day 1 (X1 = 1).

題目鏈接:https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc8/00000000002d83bf

思路

這道題需要總結一下規律,同時有多個限制,從最後一個限制開始倒推考慮。

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;

void solve2()    // 每個case裏的東西
{
    ll n,d;
    cin >> n>> d;
    vector<ll> x(n);
    for (ll i = 0; i<n; ++i){
        cin>> x[i];
    }
    ll res = d, cur = n-1;
    while(cur>=0){
        res = res - res % x[cur];
        --cur;
    }
    cout<<res;
}

int main(){
    int t = 0;
    cin >> t;
    for (int i = 1; i <= t; ++i)
    {
        cout << "Case #" << i << ": ";
        solve2();
        if(i != t)
            cout<<endl;
    }
    return 0;
}

 

3.Robot Path Decoding

Your country's space agency has just landed a rover on a new planet, which can be thought of as a grid of squares containing 109 columns (numbered starting from 1 from west to east) and 109 rows (numbered starting from 1 from north to south). Let (w, h) denote the square in the w-th column and the h-th row. The rover begins on the square (1, 1).

The rover can be maneuvered around on the surface of the planet by sending it a program, which is a string of characters representing movements in the four cardinal directions. The robot executes each character of the string in order:

  • N: Move one unit north.
  • S: Move one unit south.
  • E: Move one unit east.
  • W: Move one unit west.

There is also a special instruction X(Y), where X is a number between 2 and 9 inclusive and Y is a non-empty subprogram. This denotes that the robot should repeat the subprogram Y a total of X times. For example:

  • 2(NWE) is equivalent to NWENWE.
  • 3(S2(E)) is equivalent to SEESEESEE.
  • EEEE4(N)2(SS) is equivalent to EEEENNNNSSSS.

Since the planet is a torus, the first and last columns are adjacent, so moving east from column 109 will move the rover to column 1 and moving south from row 109 will move the rover to row 1. Similarly, moving west from column 1 will move the rover to column 109 and moving north from row 1 will move the rover to row 109. Given a program that the robot will execute, determine the final position of the robot after it has finished all its movements.

Input

The first line of the input gives the number of test cases, TT lines follow. Each line contains a single string: the program sent to the rover.

Output

For each test case, output one line containing Case #x: w h, where x is the test case number (starting from 1) and w h is the final square (w, h) the rover finishes in.

Limits

Time limit: 10 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
The string represents a valid program.
The length of each program is between 1 and 2000 characters inclusive.

Test set 1

The total number of moves the robot will make in a single test case is at most 104.

Test set 2

No additional constraints.

Sample

Input Output
4
SSSEEE
N
N3(S)N2(E)N
2(3(NW)2(W2(EE)W))
Case #1: 4 4
Case #2: 1 1000000000
Case #3: 3 1
Case #4: 3 999999995

  

In Sample Case #1, the rover moves three units south, then three units east.

In Sample Case #2, the rover moves one unit north. Since the planet is a torus, this moves it into row 109.

In Sample Case #3, the program given to the rover is equivalent to NSSSNEEN.

In Sample Case #4, the program given to the rover is equivalent to NWNWNWWEEEEWWEEEEWNWNWNWWEEEEWWEEEEW.

題目鏈接:https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc8/00000000002d83dc

思路

這道題只通過了小數據集,總體思路是先用遞歸講操作序列平鋪展開,然後在模擬操作找得到最後位置。

下面是當時的代碼:

//
//  main.cpp
//  ks_0419
//
//  Created by Zheng Xin on 2020/4/19.
//  Copyright © 2020 Zheng Xin. All rights reserved.
//


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;

ll getnum(string s, ll &idx){
    ll res = 0;
    ll i=idx;
    for(; i<s.size();++i){
        if(s[i]=='('){
            break;
        }else{
            res = res * 10 + (s[i] - '0');
        }
    }
    idx = i+1;
    return res;
}

string take(string s, ll &idx){
    ll len = s.size();
    string cmd;

    ll i = idx;
    for (; i<len; ++i){
        if(s[i]==')'){
           break;
        }
        ll cur = s[i]-'0';
        if(cur>=0 && cur<=9){
            cur = getnum(s, i); // i此時指向(
            // 取重複
            string rep = take(s, i);
            for(ll j=0; j<cur; ++j){
                cmd += rep;
            }
        }
        else{
            cmd += s[i];
        }
    }
    idx = i;
    return cmd;
}

void solve3()    // 每個case裏的東西
{
    string s;
    cin >> s;
    unordered_map<char,vector<ll>> move;
    move['S']={0,1};
    move['N']={0,-1};
    move['W']={-1,0};
    move['E']={1,0};
    unordered_set<char> dir = {'N','E','S','W'};
    ll idx = 0;
    string cmd = take(s, idx);
    ll line = 1, col = 1;
    for(int i=0;i<cmd.size(); ++i){
        line += move[cmd[i]][0];
        col += move[cmd[i]][1];
        if(line<=0){
            line = 1000000000;
        }else if(line>1000000000){
            line = 1;
        }
        if(col<=0){
            col= 1000000000;
        }else if(col>1000000000){
            col = 1;
        }
    }
    cout<<line<<" "<<col;
}

int main(){
    int t = 0;
    cin >> t;
    for (int i = 1; i <= t; ++i)
    {
        cout << "Case #" << i << ": ";
        solve3();
        if(i != t)
            cout<<endl;
    }
    return 0;
}

官方解答在遞歸時就直接向上返回各個方向移動大小的統計,然後最後對位置進行加減和處理grid是球狀的問題。

根據官方的思路實現一下:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;

ll getnum(string s, ll &idx){
    ll res = 0;
    ll i=idx;
    for(; i<s.size();++i){
        if(s[i]=='('){
            break;
        }else{
            res = res * 10 + (s[i] - '0');
        }
    }
    idx = i+1;
    return res;
}

unordered_map<char,ll> take1(string s, ll &idx){
    ll len = s.size();
    unordered_map<char, ll> res;
    ll i = idx;
    for (; i<len; ++i){
        if(s[i]==')'){
            break;
        }
        ll cur = s[i]-'0';
        if(cur>=0 && cur<=9){
            cur = getnum(s, i); // i此時指向(
            // 取重複
            auto rep = take1(s, i);
            res['E'] = (res['E'] + rep['E'] * cur) % 1000000000;
            res['S'] = (res['S'] + rep['S'] * cur) % 1000000000;
            res['N'] = (res['N'] + rep['N'] * cur) % 1000000000;
            res['W'] = (res['W'] + rep['W'] * cur) % 1000000000;
        }
        else{
            ++res[s[i]];
        }
    }
    idx = i;
    return res;
}
void solve3()    // 每個case裏的東西
{
    string s;
    cin >> s;
    ll idx = 0;
    unordered_map<char,ll> cal = take1(s,idx);
    ll line = 1 + cal['E'] - cal['W'], col = 1 + cal['S'] - cal['N'];
    if(line<=0){
        line = 1000000000 - abs(line) % 1000000000;
    }else if(line>1000000000){
        line = 1 + line % 1000000000;
    }
    if(col<=0){
        col= 1000000000 - abs(col) % 1000000000;
    }else if(col>1000000000){
        col = 1 + col % 1000000000;
    }
    cout<<line<<" "<<col;
}

int main(){
    int t = 0;
    cin >> t;
    for (int i = 1; i <= t; ++i)
    {
        cout << "Case #" << i << ": ";
        solve3();
        if(i != t)
            cout<<endl;
    }
    return 0;
}

!有一個要點:在遞歸統計的時候,爲了防止直接求和數字過大溢出,而變化大小超過grid寬度的 只有超出的部分起到實際移動作用,因此需要進行取餘。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章