[Game] algorithm code programming contest - Blue Bridge Cup simulation game 3

game:

http://oj.hzjingma.com/contest/view?id=69


 

Questions A: To be or not, this is a problem (string traversal)

Questions B: Little sharpshooter start 98K (mathematics: probability theory)

Questions C: Kuan of Dan lotus, Jia boned (or a combination of mathematical violence enumeration)

Questions D: Choudao flow of water, toast worry worry worry pin (binary enumeration)

Questions E: left hand right hand side for the round, and the world can alert Seoul (math)

Questions F: arithmetic geometric tolerances are linked through common ratio required item (thinking fast + analog + power)

Questions G: see through the whole retreat gains and losses, fame and fortune as dirt throw (greedy)

Questions H: reflected light than ten thousand yen, as treasure concept shrimp to be separated (the BFS, understand the idea, known to use BFS)

Questions I: knowing helpless rejected, miracles can occur whether or not (will not)

Questions J: end of the world, I'll take your hand (do not)

 


Questions A: To be or not, this is a problem (string traversal)

 

The use of programming, reading, and so traversal, count the number of letters. Finally, the maximum output number of letters

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

int cnt[30], A[30];

int main()
{
    memset(cnt, 0, sizeof(cnt));
    memset(A, 0, sizeof(A));

    string s;
    vector<string> passages;
    freopen("input.txt", "r", stdin);
    while(cin >> s)
    {
        passages.push_back(s);
    }

    for(auto p : passages)
    {

        for(int i = 0;i < p.size(); ++i)
        {
            // 不区分大小写
            if(p[i] >= 'a' && p[i] <= 'z') ++cnt[p[i] - 'a'];
            if(p[i] >= 'A' && p[i] <= 'Z') ++cnt[p[i] - 'A'];
        }
        cout << endl;
    }
    
    int res = 0;
    for(int i = 0;i < 26; ++i)
    {
        res = max(res, cnt[i]);
    }
    cout << res << endl;


    return 0;
}

 


Questions B: Little sharpshooter start 98K (mathematics: probability theory)

 

The desired formula: E = \ sum P_i * V_i. Wherein Here, V is the number of shots, P is the probability corresponding to

When V = 1, that is, in the first shot, then the probability is 0.75

When V = 2, not in the first shot, the second gun, then the probability is (l - 0.75) * (0.75 * 0.9)

When V = 3, the first and second gun is not in the third gun, then the probability (l - 0.75) * (l - 0.9 * 0.75) * (0.75 * 0.9 * 0.9)

Until the hit probability of <0.5 far.

So in the program, which is a variable record shot many times, several times before the record is not a variable (that is to constantly tired, not in the last probability), a variable probability that a record of. (Expectation is that each involves a number of * the probability of accumulation)

 

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

int main()
{
    int cnt = 1;  // 第几次射击
    double res = 0;
    double get = 0.75;  // 这次中的概率
    double p = 1;  // 前几次都不中概率
    while(get >= 0.5)
    {
        res += p * get * cnt;  // 第几次射击时的期望累加

        ++cnt;     // 射击次数增加
        p *= (1.0 - get);   // 相当于上一次不中,也就是累乘不中概率
        get *= 0.9;   // 这次中的概率

    }

    printf("%0.6lf\n", res);
    return 0;
}

 


Questions C: Kuan of Dan lotus, Jia boned (or a combination of mathematical violence enumeration)

 

A thought, combinatorics

That is, the time required for the four famous as a whole, that is 5,4,3,3, each as a whole, then the rest as well as 16 days (17 days of the 16 empty), then insert when the total might be: 17 * 18 * 19 * 20 = 116,280. (A 17 initially empty, insert a; 18 empty, then insert a; 19 empty, insert a; 20 empty, the last insert a)

 

Thinking two, violence enumeration

We enumerate each a masterpiece of beginning reading time, and then determine which program may be, it does not meet the full requirements. That is, each reading of this famous day, can not read the other.

We can use a variable vis, record each day whether've read, if you have read, we read, then this program is not possible.

 

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

bool isCan(int a, int b, int c, int d)
{
    int vis[40];
    memset(vis, 0, sizeof(vis));  // 记录 31 天的读书情况

    for(int i = 0; i < 5; ++i) vis[a + i] = 1;  // 第一本名著 5 天
    for(int i = 0;i < 4; ++i)  // 第二本 4 天
    {
        if(vis[b + i] == 1) return false;   // 如果这天已经读了,那么就不能再读了
        vis[b + i] = 1;
    }
    for(int i = 0;i < 3; ++i)
    {
        if(vis[c + i] == 1) return false;
        vis[c + i] = 1;
    }
    for(int i = 0;i < 3; ++i)
    {
        if(vis[d + i] == 1) return false;
        vis[d + i] = 1;
    }
    return true;
}

int main()
{
    int cnt = 0;
    
    for(int a = 1; a <= 27; ++a)  // 5
    {
        for(int b = 1; b <= 28; ++b)  // 4
        {
            for(int c = 1; c <= 29; ++c)  // 3
            {
                for(int d = 1;d <= 29; ++d)   // 3
                {
                    if(isCan(a, b, c, d)) ++cnt;
                }
            }
        }
    }

    cout << cnt << endl;
    return 0;
}

 


Questions D: Choudao flow of water, toast worry worry worry pin (binary enumeration)

 

A total number of 22, wherein the selected number of 0-12, together to form a new number.

We can use binary enumeration, for number 22, every number, only two cases take or not take, that is, 0 or 1. Therefore, a total of 2 ^ 22 is approximately equal to 4e6. It does not time out.

Because we enumerate binary, each bit corresponding to this number or not to take, if taken, would be tired and. Also note that last only take 12, so we have to judge this borrowing from the number 1, if it is> 12, that this scheme does not hold.

Then calculate the number of all cases, with set statistics (there may be duplicates, de-emphasis).

Finally, the answer is to ask, can not constitute the number, so the answer is: Total (1695) - the number set in (so may constitute a majority)

 

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

int nums[22] = {3,5,7,11,13,19,23,29,31,37,41,53,59,61,67,71,97,101,127,197,211,431};

set<int> recd;

int main()
{
    recd.clear();
    int lim = (1 << 22);

    for(int i = 0;i < lim; ++i)
    {
        int cnt = 0;
        int res = 0;
        int tep = i;
        for(int j = 0;j < 22; ++j)
        {
            if((tep >> j) & 1)
            {
                ++cnt;
                res += nums[j];
            } 
        }
        if(cnt <= 12) 
        {
            recd.insert(res);
        }
    }
    int cnt = 1695;
    cout << cnt - recd.size() << endl;

    return 0;
}

Questions E: left hand right hand side for the round, and the world can alert Seoul (math)

According to subject, that is, the calculated value, we can see that by drawing

I.e. four times, four times a sector area of ​​a triangle area.

Seeking good area of ​​a triangle, two sides, a bottom seeking the Pythagorean theorem, and S = 1/2 * * High bottom

For the sector area:  \frac{1}{2}\Theta R^2where [theta] is the angle in radians, we can find that angle in radians triangle (cosA = 4/3), to give after A, we found that A + A + θ = PI / 2. Then you can find a θ.

The rest is to find the value of the Code

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

const double PI = 3.14159265358979;

int main()
{
    double res = 3.0 * sqrt(7.0) + 8.0 * (PI / 2.0 - 2.0 * acos(3.0 / 4.0));

    printf("%0.2lf\n", res);
    return 0;
}

 


Questions F: arithmetic geometric tolerances are linked through common ratio required item (thinking fast + analog + power)

We v1 = b / a, v2 = c / b. Due to the maximum, it requires out of the common ratio q as large as possible

Which, v1 and v2 should be to find one of the largest power of q can satisfy = v1, and = v2.

A few special cases:

1) When b = a, b, or c = the time, the common ratio is q = 1

2) If v1 == v2, it can be directly q = v1.

The remaining case is that we already know v1 (the minimum v1 and v2 are stored in v1), would not be common ratio, then the most likely should start looking from prescribing (because it would not be q = v1 , then as large as possible in order to make q, then q ^ 2 = v1, q = sqrt (v1), up to 2, enum q)

When it is judged that q and v1, v2 is a power relationship (that is, for example, q ^ x = v1, that is to say, v1% q == 0, up until v1 == 1. Or is not satisfied with q ^ x = v1)

After the final resulting geometric progression q, then the item N, according to the formula, is an = a1 * q ^ (n - 1).

Since the data range n, so we want to use the fast power to resolve.

 

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

#define LL long long

const LL MOD = 1e9;

LL qpow(LL x, LL y)
{
    LL ans = 1;
    while(y)
    {
        if(y & 1) ans = ans * x % MOD;
        x = x * x % MOD;
        y >>= 1;
    }
    return ans;
}

bool isCan(LL x, LL y)  // x == y ^ n
{
    while(x)
    {
        if(x == 1) return true;
        if(x % y != 0) return false;

        x /= y;
    }
}

int main()
{
    LL a, b, c, N;
    cin >> a >> b >> c >> N;

    if(a == b || a == c) cout << a << endl;
    else
    {
        LL res = 0, q = 0;
        LL v1 = b / a, v2 = c / b;

        if(v1 > v2) swap(v1, v2);

        if(v1 == v2) q = v1;
        else
        {
            q = v1;
            int temp = sqrt(v1);

            for(int i = temp + 1; i >= 2; --i)
            {
                if(isCan(v1, i) && isCan(v2, i))
                {
                    q = i;
                    break;
                }
            }
        }

        res = a * qpow(q, N - 1) % MOD;
        cout << res << endl;
    }

    return 0;
}

Questions G: see through the whole retreat gains and losses, fame and fortune as dirt throw (greedy)

 

We demand a particular day, at this time there may be a few days before the leftovers, then the first with the surplus goods replenishment.

If replenishment, and worse, they would have to buy a new

As for buying, since you can buy a lift, we find that day and in front of all the most expensive (like this makes the minimum cost, and can be, because this way, we are ahead of the equivalent in front buy).

So, for the purchase price, we have been looking for this day and the minimum price for all the previous purchase can be.

 

Of particular note, due to buy a box lemons was 5 * 16 = 80, all pay attention. Meanwhile, to buy a new lemon, if you can not be divisible by 80, that we want to buy a little, but this way, there will be leftovers (to record)

 

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

#define LL long long

int main()
{
    LL n, a, b;
    cin >> n >> a >> b;
    
    LL miPa = 1000, miPb = 1000;
    LL hasPa = 0, hasPb = 0;

    LL res = 0;

    for(int i = 0;i < n; ++i)
    {
        LL c, pa, pb;
        cin >> c >> pa >> pb;

        miPa = min(miPa, pa);
        miPb = min(miPb, pb);

        res += c * a * miPa;

        if(hasPb >= c * b)  // 剩货超过需要的,那么不需要重新买
        {
            hasPb -= c * b;
        }
        else  // 否则,说明不够,要重新买
        {
            LL needPb = c * b - hasPb;
            hasPb = 0;

            if(needPb % 80 == 0)
            {
                res += (needPb / 80) * miPb;
                
            }
            else
            {
                LL tep = (needPb / 80) + 1;   // 不被整除,说明多买一盒
                res += tep * miPb;
                hasPb = tep * 80 - needPb;   // 多出来的剩货,要记录
            }
        }

    }
    cout << res << endl;

    return 0;
}

Questions H: reflected light than ten thousand yen, as treasure concept shrimp to be separated (the BFS, understand the idea, known to use BFS)

 

This question is to know with BFS, but do not know how the kind of thinking.

Code can see links submitted by others, but the idea did not know, so I do not understand.

 


Questions I: knowing helpless rejected, miracles can occur whether or not (will not)

 

Code can see links submitted by others, but the idea did not know, so I do not understand.


Questions J: end of the world, I'll take your hand (do not)

 

Code can see links submitted by others, but the idea did not know, so I do not understand.

Published 136 original articles · won praise 126 · views 110 000 +

Guess you like

Origin blog.csdn.net/Mikchy/article/details/104581598