【kick start】2020 A round

230 篇文章 0 订阅
9 篇文章 0 订阅

这里的代码只贴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

InputOutput
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

InputOutput
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 1Output 1
1
3 1
100 200 230
Case #1: 50  
Input 2Output 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值