第十三週比賽

6822 Black and white stones
Shagga and Dolf like to play a game with stones, each of which is either black or white. At the beginning
of the game, Dolf arranges all the stones in a single line from left to right. Then, Shagga’s goal is to
reorder the stones so that all the black stones are to the left of all the white stones. To do this, he
can choose any pair of stones of different color and swap their positions, paying A coins to Dolf in the
process. However, if the two stones whose positions he is swapping are adjacent, Dolf must give him a
refund of B coins, meaning that the operation will effectively cost Shagga only A − B coins.
Shagga is not very bright, so he hasn’t realized yet that he will only loose coins while playing this
game. However, he is aware of his limitations, so he knows that if he played optimally he would loose
fewer coins than he is loosing right now, with his strategy of randomly choosing the stones he swaps in
each movement. Therefore, he wants to know the minimum number of coins he will have to pay Dolf
in order to get to the desired arrangement of stones, and is threatening to feed you to the goats if you
don’t help him.
Input
The input contains several test cases; each test case is formatted as follows. The first line contains two
integers A and B (0 ≤ B < A ≤ 106
), representing respectively the cost of swapping two stones and
the value of the refund when swapping adjacent stones. The second line contains a non-empty string
S of at most 5000 characters. The i-th character of S indicates the color of the i-th stone, from left to
right, in the initial arrangement of the stones. The character is either the uppercase letter ‘B’ or the
uppercase letter ‘W’, indicating respectively a black or a white stone.
Output
For each test case in the input, output a line with an integer representing the minimum number of coins
Shagga will have to pay Dolf in order to arrange the stones such that all the black ones are to the left
of all the white ones.
Sample Input
2 1
BWWB
5 3
WBWWBWBWBWBBBWWBBB
1000000 0
W
Sample Output
2
27
0
題意:第一行數據n,m,交換不相鄰的數需要花費n元,交換相鄰的數需要花費n-m元
問最小花費
思路:計算出b,w的個數,b的最右側和w的最左側開始查詢,每次找出b和w位置中不屬於b和w的標,然後求出他們每次的最優解。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#define PI acos(-1)
const int mod = 1001113;
const int maxx = 1e9;
typedef long long LL;
using namespace std;
int main()
{
    LL a,b,c;
    string s;
    while(cin>>a>>b)
    {
        cin>>s;
        LL lb=0,lw=0,ans=0,ant=0;
        LL len;
        len=s.size();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='B')
                ant++;
        }
        lb=ant-1;
        lw=ant;
        c=a-b;
        while(1)
        {
            while(s[lb]!='W'&&lb>=0)
                lb--;
            if(lb<0)
                break;
            while(s[lw]!='B'&&lw<len)
                lw++;
            ans+=min(a,c*(lw-lb));
            lb--,lw++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

C - Counting substhreengs
Substrings are strings formed by choosing a subset of contiguous characters from a string. This is well
known. A little more obscure is the definition of substhreengs. A substhreeng is a substring which
complies to the following additional requirements:

  1. It is non-empty, and composed entirely of base 10 digits.
  2. Interpreted in base 10 (allowing extra leading zeros), the resulting integer is a multiple of 3.
    For instance, the string “130a303” contains 9 substhreengs: the substhreeng ‘3’ three times, the
    substhreengs ‘30’ and ‘0’ twice each, and the substhreengs ‘303’ and ‘03’ once each. The substring
    ‘30a3’ is not a substhreeng because it is not composed entirely of base 10 digits, while the substring
    ‘13’ is not a substhreeng because 13 is not a multiple of 3.
    Notice that two substhreengs are considered different if they are different in length or start at a
    different position, even if the selected characters are the same.
    Given a string, you are asked to count the number of substhreengs it contains.
    Input
    The input contains several test cases; each test case is formatted as follows. A test case consists of a
    single line that contains a non-empty string S of at most 106
    characters. Each character of S is either
    a digit or a lowercase letter.
    Output
    For each test case in the input, output a line with an integer representing the number of substhreengs
    contained in S.
    Sample Input
    130a303
    0000000000
    icpc2014regional
    Sample Output
    9
    55
    2
    題意:找出他們的子串並且能夠被3整除。
    思路:能被三整除的數的特徵是什麼 ? 各個數位上的數字之和是3的倍數.
    所以說我們需要定義一個num數組,分別計算每個字串和爲0,1,2的個數。定義x,y,z分別計算當前字串被3求餘分別爲0,1,2數量。
    然後對每一個字符求餘3的出餘數0,1,2,當求餘爲1時,x=num[2],y=num[1],z=num[0]+1;
    依次類推,,,,
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define PI acos(-1)
const int mod = 1001113;
const int maxx = 1e6 + 10;
typedef long long LL;
using namespace std;
int main()
{
    string s;
    LL len,num[4]= {0};
    while(cin>>s)
    {
        LL ans=0;
        LL k=0;
        LL x,y,z;
        len=s.size();
        memset(num,0,sizeof(num));
        for(int i=0; i<len; i++)
        {
            if(s[i]>='0'&&s[i]<='9')
            {
                k=(s[i]-'0')%3;
                if(k==1)
                {
                    x=num[2];
                    y=num[0]+1;
                    z=num[1];
                }
                else if(k==2)
                {
                    x=num[1];
                    y=num[2];
                    z=num[0]+1;
                }
                else
                {
                    x=num[0]+1;
                    y=num[1];
                    z=num[2];
                }
                num[0]=x;
                num[1]=y;
                num[2]=z;
                ans+=num[0];
            }
            else
            {
                num[0]=0;
                num[1]=0;
                num[2]=0;
            }

        }
        cout<<ans<<endl;
    }
    return 0;
}

H - Help cupid
Cupid’s job is getting harder, so he is adopting new technologies to help him with his difficult task of
matching people into happy couples. He appointed the best programmers in his staff to a new project
called Advanced Couples Matching (ACM). For this project, the programmers need to produce an
algorithm that takes a set of an even number of N lonely persons and matches them into N/2 couples,
such that each person is in exactly one couple.
Sadly, the data available about each person is limited. In this modern world, using gender, ethnicity,
age or nationality as criteria to form couples is not a sensible option, so the programmers can only use
data about the internet connection of each candidate. They decided to focus this stage on time zones.
People living in closer time zones are more likely to find time to interact with each other. Thus, the
programmers decided to create couples so as to minimize the total time difference.
Each time zone is identified by an integer between -11 and 12, inclusive, representing its difference
in hours from a particular time zone called Coordinated Universal Time (or UTC). The time difference
of two people living in time zones represented by integers i and j is the minimum between |i − j| and
24 − |i − j|. Given a partition of a set of an even number N of candidates into N/2 couples, its total
time difference is the sum of the time difference of each couple.
You are asked to write a program that receives as input the time zones of a set of N candidates.
The output of the program must be the minimum total time difference among all possible partitions of
the set into couples.
Input
The input contains several test cases; each test case is formatted as follows. The first line contains an
even integer N (2 ≤ N ≤ 1000) representing the number of candidates to be coupled. The second line
contains N integers T1, T2, …, TN (−11 ≤ Ti ≤ 12 for i = 1, 2, . . . , N) indicating the time zones of the
candidates.
Output
For each test case in the input, output a line with an integer representing the minimum total time
difference among all possible partitions of the set of candidates into couples.
Sample Input
6
-3 -10 -5 11 4 4
2
-6 6
8
0 0 0 0 0 0 0 0
Sample Output
5
12
0
題意:給你一個n個數,找最小的一個值,每兩個數要麼是abs(i-j),要麼是 24-abs(i-j),求出最小的和。
思路:1、首先sort一下
2、假設有6個數,分別爲1 2 3 4 5 6,我們進行相減的時候,分別是2-1,4-3,6-5然後得出一個值,因爲題目上給的時間是循環的,所以我們可以考慮把它當作一個環來看。然後把1放在6後面,我們可以得出新的序列2 3 4 5 6 1,相減分別爲3-2 ,5-4,6-1,如果我們再把2放在後面,發現他們和第一個序列是一樣的,所以我們只需要換一次,分別比較兩個序列的最小值即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#define PI acos(-1)
const int mod = 1001113;
const int maxx = 1e9;
typedef long long LL;
using namespace std;
int main()
{
    int n;
    int a[1010],b[1010];
    while(cin>>n)
    {
        int ans=0,ans1=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(a,a+n);
        for(int i=0;i<n-1;i+=2)
        {
            ans+=min(a[i+1]-a[i],24-(a[i+1]-a[i]));
        }
        for(int i=1;i<n-2;i+=2)
        {
            ans1+=min(a[i+1]-a[i],24-(a[i+1]-a[i]));
        }
        ans1+=min(a[n-1]-a[0],24-(a[n-1]-a[0]));
        cout<<min(ans,ans1)<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章