【kick start】2020 A round

這裏的代碼只貼solution函數,代碼框架請看這篇:kick start代碼框架

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

賽後總結

這輪打的太坎坷啦。

題目很簡單,但是除了各種狀況。

首先是第一題卡在輸出格式上,不知道爲什麼使用以前的代碼竟然還能出錯,搞了一小時的輸出格式才弄對。

接着一直有人干擾做題,叫我幫忙幹別的事。。。我也是服了。

最終只有前兩題ac,第三題剛寫出代碼。

不足&改進

1.代碼結構出錯。需要記錄代碼框架,方便複用。

2.dp思路不夠快。dp需要繼續加強。

3.複雜代碼實現不夠快。今後做難題的時候要計時,並且避免出現低級錯誤。

4.提高長英文題閱讀速度。做leetcode的時候堅決要讀英文題。

優點

1.相比幾個同學,編程能力還是不錯滴。

 

題目

由於時間原因,最近忙於面試,第4題暫先不分析。

1. Allocation

Problem

There are N houses for sale. The i-th house costs Ai dollars to buy. You have a budget of B dollars to spend.

What is the maximum number of houses you can buy?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case begins with a single line containing the two integers N and B. The second line contains N integers. The i-th integer is Ai, the cost of the i-th house.

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 maximum number of houses you can buy.

Limits

Time limit: 15 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ B ≤ 105.
1 ≤ Ai ≤ 1000, for all i.

Test set 1

1 ≤ N ≤ 100.

Test set 2

1 ≤ N ≤ 105.

Sample

Input Output
3
4 100
20 90 40 90
4 50
30 30 10 10
3 300
999 999 999
Case #1: 2
Case #2: 3
Case #3: 0

In Sample Case #1, you have a budget of 100 dollars. You can buy the 1st and 3rd houses for 20 + 40 = 60 dollars.
In Sample Case #2, you have a budget of 50 dollars. You can buy the 1st, 3rd and 4th houses for 30 + 10 + 10 = 50 dollars.
In Sample Case #3, you have a budget of 300 dollars. You cannot buy any houses (so the answer is 0).

Note: Unlike previous editions, in Kick Start 2020, all test sets are visible verdict test sets, meaning you receive instant feedback upon submission.

題目鏈接:https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f56

思路

非常滴簡單。因爲只需要買的東西越多越好,那麼直接按照東西單價取便宜的即可。

因此做法是:按價格遞增排序->按順序買。

void solve(){
    int n, b;
    cin>>n>>b;
    vector<int> price;
    int x, ans = 0;
    for(int i = 0; i < n; ++i){
        cin>>x;
        price.push_back(x);
        
    }
    sort(price.begin(), price.end());
    for(int i = 0; i < price.size(); ++i){
        if(b < price[i])
            break;
        ++ans;
        b -= price[i];
    }
    cout<<ans;
}

 

2. Plates

Problem

Dr. Patel has N stacks of plates. Each stack contains K plates. Each plate has a positive beauty value, describing how beautiful it looks.

Dr. Patel would like to take exactly P plates to use for dinner tonight. If he would like to take a plate in a stack, he must also take all of the plates above it in that stack as well.

Help Dr. Patel pick the P plates that would maximize the total sum of beauty values.

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 three integers NK and P. Then, N lines follow. The i-th line contains K integers, describing the beauty values of each stack of plates from top to bottom.

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 maximum total sum of beauty values that Dr. Patel could pick.

Limits

Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ K ≤ 30.
1 ≤ P ≤ N * K.
The beauty values are between 1 and 100, inclusive.

Test set 1

1 ≤ N ≤ 3.

Test set 2

1 ≤ N ≤ 50.

Sample

Input Output
2
2 4 5
10 10 100 30
80 50 10 50
3 2 3
80 80
15 50
20 10
Case #1: 250
Case #2: 180  

In Sample Case #1, Dr. Patel needs to pick P = 5 plates:

  • He can pick the top 3 plates from the first stack (10 + 10 + 100 = 120).
  • He can pick the top 2 plates from the second stack (80 + 50 = 130) .

In total, the sum of beauty values is 250.

In Sample Case #2, Dr. Patel needs to pick P = 3 plates:

  • He can pick the top 2 plates from the first stack (80 + 80 = 160).
  • He can pick no plates from the second stack.
  • He can pick the top plate from the third stack (20).

In total, the sum of beauty values is 180.

Note: Unlike previous editions, in Kick Start 2020, all test sets are visible verdict test sets, meaning you receive instant feedback upon submission.

題目鏈接:https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d40bb

思路

這題不難看出需要用dp,但如何dp需要多考慮。

從第一堆盤子開始看,狀態由2個因素組成:當前位於第幾堆第幾個,當前還能取多少盤子。

由於盤子必須全取,因此每次考慮當前盤子是否取的時候,都是在考慮當前這一堆取到這裏的價值,和以前其他情況比較。

所以可以嘗試着建立dp:用於儲存在第i堆取盤子,剩餘j個給後面堆去處理的情況下,最大的價值。

狀態轉移方程:

dp[0][j]=bt[k-j],j=0,1,...,min(k,p)dp[0][j] = value[k-j], j=0,1,...,min(k,p)

dp[i][j] = max(dp[i-1][j], dp[i-1][j-a]+bt[a]), a=1,2,...,min(k, p)

bt數組用於儲存:取到第a個時,這一堆的價值。

對於dp數組後續還可以空間優化:只需要記錄上一堆的情況即可。

void solve(){
    ll n, k, p;
    cin>> n>> k>> p;
    vector<vector<ll>> bt(n, vector<ll>(k+1,0));
    ll x, sum = 0;
    for(ll j=0; j<n;++j){
        sum = 0;
        for(ll i = 1; i <= k; ++i){
            cin>> x;
            sum += x;
            bt[j][i] = sum;
        }
    }
    vector<vector<ll>> dp(n, vector<ll>(p+1, 0));
    for(ll j=p; j>=0; --j){
        if(p-j>=0 && p-j<=k) dp[0][j] = bt[0][p-j];
        else {
            dp[0][j] = 0;
        }
    }
    for(ll i=1; i<n; ++i){
        for(ll j=p; j>=0; --j){ //剩餘j個
            ll take = p-j;  // 一共取take個
            if(take==0){
                dp[i][j]= 0;
                continue;
            }
            dp[i][j] = dp[i-1][j];
            ll nowtake = min(take,k);
            for(ll a=1; a<=nowtake; ++a){ //本行取a個,以前取take-a個 以前剩j+1個
                dp[i][j] = max(dp[i][j], (dp[i-1][j+a] + bt[i][a]));
            }
        }
    }
    cout<<dp[n-1][0];
}

 

3. Workout

Problem

Tambourine has prepared a fitness program so that she can become more fit! The program is made of N sessions. During the i-th session, Tambourine will exercise for Mi minutes. The number of minutes she exercises in each session are strictly increasing.

The difficulty of her fitness program is equal to the maximum difference in the number of minutes between any two consecutive training sessions.

To make her program less difficult, Tambourine has decided to add up to K additional training sessions to her fitness program. She can add these sessions anywhere in her fitness program, and exercise any positive integer number of minutes in each of them. After the additional training session are added, the number of minutes she exercises in each session must still be strictly increasing. What is the minimum difficulty possible?

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 K. The second line contains N integers, the i-th of these is Mi, the number of minutes she will exercise in the i-th session.

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 minimum difficulty possible after up to K additional training sessions are added.

Limits

Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
For at most 10 test cases, 2 ≤ N ≤ 105.
For all other test cases, 2 ≤ N ≤ 300.
1 ≤ Mi ≤ 109.
Mi < Mi+1 for all i.

Test set 1

K = 1.

Test set 2

1 ≤ K ≤ 105.

Samples

Input 1 Output 1
1
3 1
100 200 230
Case #1: 50  
Input 2 Output 2
3
5 2
10 13 15 16 17
5 6
9 10 20 26 30
8 3
1 2 3 4 5 6 7 10
Case #1: 2
Case #2: 3
Case #3: 1  

Sample #1

In Case #1: Tambourine can add up to one session. The added sessions are marked in bold: 100 150 200 230. The difficulty is now 50.

Sample #2

In Case #1: Tambourine can add up to six sessions. The added sessions are marked in bold: 9 10 12 14 16 18 20 23 26 29 30. The difficulty is now 3.

In Case #2: Tambourine can add up to three sessions. The added sessions are marked in bold: 1 2 3 4 5 6 7 8 9 10. The difficulty is now 1. Note that Tambourine only added two sessions.

  • Note #1: Only Sample #1 is a valid input for Test set 1. Consequently, Sample #1 will be used as a sample test set for your submissions.
  • Note #2: Unlike previous editions, in Kick Start 2020, all test sets are visible verdict test sets, meaning you receive instant feedback upon submission.

題目鏈接:https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f5b

思路

原本的思路:要求的是數據中相鄰最大差 最小,那麼對差進行排列,不斷分裂最大的差即可(即把額外課程加入最大差的正中間)。

這裏出現一個問題:插入數量大於1時,平均插入比折半插入效果好。

eg:假設課程是[2,12,15]

k=1,那麼插入後[2,7,12,15],max=6;

k=2時,如果按照不斷二分的思路來看插入變爲[2,7,9,12,15],max=6,如果採用平均插入則變爲[2,5,8,12,15],max=4。

因此可以反向思維:若初始的最大差爲d,那麼插入課程後可優化的範圍就在[1,d)內,所以對這個區間進行二分搜索,不斷嘗試看是否能對超出要求的時間差完成優化。

bool check(vector<ll> &di, ll mid, ll k){
    ll len = di.size();
    for(ll j=0; j<len; ++j){
        if(di[j]<=mid) return true;
        else if(k<=0) return false;     // 沒法繼續剖了
        ll cut = ceil(double(di[j])/(mid))-1;
        k -= cut;
        if(k<0) return false;
    }
    return true;
}
void solve(){
    ll n, k;
    cin>>n>>k;
    vector<ll> di;
    ll last,now;
    for(ll j=0; j<n;++j){
        if(j==0){
            cin>>last;
        }
        else {
            cin>> now;
            di.push_back(now-last);
            last = now;
        }
    }
    sort(di.begin(),di.end(), greater<ll>());
    ll l=1, r=di[0]-1;
    while(l<=r){
        ll mid = l + (r-l)/2;
        if(check(di, mid, k)){
            r = mid - 1;
        }else{
            l = mid + 1;
        }
    }
    cout<<l;
}

 

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