code force A. Bear and Five Cards&B. Bear and Finding Criminals

来源:互联网 发布:sqlserver软件下载 编辑:程序博客网 时间:2024/06/09 15:17

A. Bear and Five Cards
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer.

Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards.

He is allowed to at most once discard two or three cards with the same number. Of course, he won’t discard cards if it’s impossible to choose two or three cards with the same number.

Given five numbers written on cards, cay you find the minimum sum of numbers on remaining cards?

Input
The only line of the input contains five integers t1, t2, t3, t4 and t5 (1 ≤ ti ≤ 100) — numbers written on cards.

Output
Print the minimum possible sum of numbers written on remaining cards.

Examples
input
7 3 7 3 20
output
26
input
7 9 3 1 8
output
28
input
10 10 10 10 10
output
20
Note
In the first sample, Limak has cards with numbers 7, 3, 7, 3 and 20. Limak can do one of the following.

Do nothing and the sum would be 7 + 3 + 7 + 3 + 20 = 40.
Remove two cards with a number 7. The remaining sum would be 3 + 3 + 20 = 26.
Remove two cards with a number 3. The remaining sum would be 7 + 7 + 20 = 34.
You are asked to minimize the sum so the answer is 26.

In the second sample, it’s impossible to find two or three cards with the same number. Hence, Limak does nothing and the sum is 7 + 9 + 1 + 3 + 8 = 28.

In the third sample, all cards have the same number. It’s optimal to discard any three cards. The sum of two remaining numbers is 10 + 10 = 20.

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<map>typedef long long LL;using namespace std;int a[1111];int main(){    //while (1)    {        int Q[1111] = { 0 };        for (int i = 0; i < 5; i++)        {            cin >> a[i];            Q[a[i]]++;        }        //for (int i = 0; i < 5; i++)        //  cout << Q[a[i]] << endl;        int sum = 0, f = 0, k = 0;        for (int i = 0; i < 5; i++)        {            sum += a[i];            if (Q[a[i]] == 1)            {                k++;            }            if (a[i] != a[0])                f = -1;        }        if (f == 0)        {            //puts("1");            cout << a[0] * 2 << endl;        }        else if (k == 5)            cout << sum << endl;        else        {            //cout << sum << endl;            int ss;            int mm = 999999;            //puts("jinru");            for (int i = 0; i < 5; i++)            {                if (Q[a[i]] > 1)                {                    if (Q[a[i]] > 3)                        Q[a[i]] = 3;                    ss = sum - a[i] * Q[a[i]];                    Q[a[i]] = 0;                    //cout << s << endl;                    if (ss < mm)                    {                        mm = ss;                        //cout << mm << endl;                    }                }            }            cout << mm << endl;        }    }    return 0;}

B. Bear and Finding Criminals
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.

Limak is a police officer. He lives in a city a. His job is to catch criminals. It’s hard because he doesn’t know in which cities criminals are. Though, he knows that there is at most one criminal in each city.

Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.

You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.

Input
The first line of the input contains two integers n and a (1 ≤ a ≤ n ≤ 100) — the number of cities and the index of city where Limak lives.

The second line contains n integers t1, t2, …, tn (0 ≤ ti ≤ 1). There are ti criminals in the i-th city.

Output
Print the number of criminals Limak will catch.

Examples
input
6 3
1 1 1 0 1 0
output
3
input
5 2
0 0 0 1 0
output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.

Using the BCD gives Limak the following information:

There is one criminal at distance 0 from the third city — Limak is sure that this criminal is exactly in the third city.
There is one criminal at distance 1 from the third city — Limak doesn’t know if a criminal is in the second or fourth city.
There are two criminals at distance 2 from the third city — Limak is sure that there is one criminal in the first city and one in the fifth city.
There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.

In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak’s city. There is only one city at distance 2 so Limak is sure where a criminal is.

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<map>typedef long long LL;using namespace std;int a[1111];int main(){    int n, idx;    while (cin >> n >> idx)    {        int aa;        for (int i = 0; i < n; i++)        {            cin >> aa;            a[i] = aa;        }        int s = 0;        idx--;        if (a[idx]) s++;        int l = idx - 1, r = idx + 1;        while (true)        {            // cout << "l "<<l << endl;            //  cout << "r " << r << endl;            if ((l >= 0 && a[l]))            {                s++;                //cout << "s " << s << endl;            }            if (r < n&&a[r])                s++;            if ((l>=0&&r<n)&&(a[l] + a[r] == 1))            {                s--;            }            if (l >= 0)                l--;            if (r < n)                r++;            if (l < 0 && r >= n)                break;        }        cout << s << endl;    }    return 0;}
原创粉丝点击