【Codeforces】1217A – Creating a Character

正文索引 [隐藏]

Problem Description:

You play your favourite game yet another time. You chose the character you didn’t play before. It has strstr points of strength and intint points of intelligence. Also, at start, the character has expexp free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by 1 or raise intelligence by 1).

Since you’d like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).

Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.

Input Specification:

The first line contains the single integer T (1 ≤ T ≤ 100) — the number of queries. Next T lines contain descriptions of queries — one per line.

This line contains three integers strstr, intint and expexp (1 ≤ str,int ≤ \inline 10^{8}, 0 ≤ exp ≤ 10^{8}) — the initial strength and intelligence of the character and the number of free points, respectively.

Output Specification:

Print T integers — one per query. For each query print the number of different character builds you can create.

Sample Input:

4
5 3 4
2 1 0
3 5 5
4 10 6

Sample Output:

3

1

2

0

Note:

In the first query there are only three appropriate character builds: (str=7,int=5), (8,4) and (9,3). All other builds are either too smart or don’t use all free points.

In the second query there is only one possible build: (2,1).

In the third query there are two appropriate builds: (7,6), (8,5).

In the fourth query all builds have too much brains.

解题思路:

这题大意是分配exp点经验给力量s和智力i,求有多少种分配情况使s比i高。我一开始是模拟了这个分配的过程提交代码直接TLE。分情况考虑:①当s>i+e时,有e+1种情况;②当s<i时 俩者相差i-s,先将s与i补齐,即e-(i-s) = e-i+s,然后再除以2向上取整,若s

+e<=i就会出现负数,此时怎么点经验都是徒劳,所以要拿e-i+s和0进行比较取最大值;③当s>=i时 俩者相差s-i,则(s-i+e)除以2再向上取整。最后取三种情况的最大值即可。

AC代码:TLE代码:

#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int s,i,exp,cnt = 0;
        cin >> s >> i >> exp;
        Up(j,0,exp)
        {
            int _s = s+j;
            int _i = i+exp-j;a
            if(_s > _i)
            {
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

AC代码:

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

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int s,i,e,cnt = 0;
        cin >> s >> i >> e;
        int _ = ceil((e-i+s)/2.0);
        cout << min(e+1, max(_,0)) << endl;   //当s>i+e时就是e+1
    }
    return 0;
}