第十三周比赛

本文介绍了一个有趣的游戏情侣匹配挑战,旨在通过最小化时间差异来创建最佳配对。参与者需对一组数字进行排序,并采用特定策略计算两组数字的最小总差异,展示了算法优化在现代情侣匹配中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值