Codeforces Round 933 (Div. 3) A-C题(暴力算法)

                                A. Rudolf and the Ticket

Rudolf is going to visit Bernard, and he decided to take the metro to get to him. The ticket can be purchased at a machine that accepts exactly two coins, the sum of which does not exceed k .

Rudolf has two pockets with coins. In the left pocket, there are n coins with denominations b1​,b2​,…,bn​ . In the right pocket, there are coins with denominations c1​,c2​,…,cm​ . He wants to choose exactly one coin from the left pocket and exactly one coin from the right pocket (two coins in total).

Help Rudolf determine how many ways there are to select indices and s such that bf​+cs​≤k .

输入格式

The first line contains an integer t ( 1≤t≤100 ) — the number of test cases. Then follows the description of each test case.

The first line of each test case contains three natural numbers n , m , and k (1≤n,m≤100,1≤k≤2000 ) — the number of coins in the left and right pockets, and the maximum sum of two coins for the ticket payment at the counter, respectively.

The second line of each test case contains n integers bi​ ( 1≤bi​≤1000 ) — the denominations of coins in the left pocket.

The third line of each test case contains m integers ci​ (1≤ci​≤1000 ) — the denominations of coins in the right pocket.

输出格式

For each testcase, output a single integer — the number of ways Rudolf can select two coins, taking one from each pocket, so that the sum of the coins does not exceed k .

输入输出样例

输入 

4
4 4 8
1 5 10 14
2 1 8 1
2 3 4
4 8
1 2 3
4 2 7
1 1 1 1
2 7
3 4 2000
1 1 1
1 1 1 1

输出 

6
0
4
12

说明/提示

Note that the pairs indicate the indices of the coins in the array, not their denominations.

In the first test case, Rudolf can choose the following pairs of coins: [1,1],[1,2],[1,4],[2,1],[2,2],[2,4][1,1],[1,2],[1,4],[2,1],[2,2],[2,4] .

In the second test case, Rudolf cannot choose one coin from each pocket in any way, as the sum of any two elements from the first and second arrays will exceed the value of k=4 .

In the third test case, Rudolf can choose: [1,1],[2,1],[3,1],[4,1][1,1],[2,1],[3,1],[4,1] .

In the fourth test case, Rudolf can choose any coin from the left pocket and any coin from the right pocket.

此题可以理解为有一个东西价值为K,从两个口袋里面任意各拿出一个硬币,如果俩个硬币的和小于等于K,则可以肯定它的价值,如果大于则跳过即可,直接上代码

P:因为题的数据不大,可以直接暴力

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

int main(){
    int T;
    cin >> T;

    int n,m,k;
    while(T--){
        cin >> n >> m >>k;
        int a[n],b[m],ans=0;//开两个数组计入俩个口袋,ans计入答案
        for(int i=0;i<n;i++) cin >>a[i];
        for(int i=0;i<m;i++) cin >>b[i];

        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(a[i]+b[j]<=k) ans++; //直接暴力搜索,符合题意直接计入
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

B. Rudolf and 121

Rudolf has an array a of n integers, the elements are numbered from 1 to n .

In one operation, he can choose an index i (2≤i≤n−1 ) and assign:

  • ai−1​=ai−1​−1
  • 2ai​=ai​−2
  • ai+1​=ai+1​−1

Rudolf can apply this operation any number of times. Any index i can be used zero or more times.

Can he make all the elements of the array equal to zero using this operation?

输入格式

The first line of the input contains a single integer t ( 1≤t≤104 ) — the number of test cases in the test.

The first line of each case contains a single integer n ( 3≤n≤2⋅105 ) — the number of elements in the array.

The second line of each case contains n integers 1a1​,a2​,…,an​ ( 0≤aj​≤109 ) — the elements of the array.

It is guaranteed that the sum of the values of n over all test cases does not exceed 2⋅1052⋅105 .

输出格式

For each test case, output "YES" if it is possible to make all the elements of the array zero using the described operations. Otherwise, output "NO".

You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.

输入输出样例

输入 

7
5
1 3 5 5 2
5
2 4 4 5 1
5
0 1 3 3 1
6
5 6 0 2 3 0
4
1 2 7 2
3
7 1 0
4
1 1 1 1

输出 

YES
NO
YES
NO
NO
NO
NO

说明/提示

此题两边的数只能减1,如果第一个数大于第二个那么可以直接输出NO,第一个数减空,那么第二个数则减第一个数的2倍,所以还得满足第二个条件,第一个数的2倍小于等于第二个数。

样例演示

1 3 5 5 2 //满足俩个条件第一此减

0 1 4 5 2

0 0 2 4 2

0 0 0 0 0

2 4 4 5 1//此时满足俩个条件

0 0 2 5 1 //也满足两个条件

0 0 0 1 -1 //此时不可能全为0

直接上代码

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

const int N=2e5+10;
int a[N];//开个大数组
int main(){
    int T;
    cin >> T;
    int n;
    while(T--){
        cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n-2;i++){
        if(a[i]<a[i+1] && 2*a[i]<=a[i+1])//此时的俩个条件,需满足这俩才进行减
        {
           a[i+1]=a[i+1]-2*a[i];//第二个数减去第一个数的2倍
           a[i+2]=a[i+2]-a[i];/第三个数减第一个数
           a[i]=0;//第一个数置0
        }
    }
        int flag=1; //定义一个变量
        for(int i=1;i<=n;i++){
           if(a[i]!=0) flag=0;//循环整个数组,如果有非0则flag==0
        }
        if(flag==1) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
}
 return 0;
}

C. Rudolf and the Ugly String

Rudolf has a string s of length n . Rudolf considers the string s to be ugly if it contains the substring †† "pie" or the substring "map", otherwise the string s will be considered beautiful.

For example, "ppiee", "mmap", "dfpiefghmap" are ugly strings, while "mathp", "ppiiee" are beautiful strings.

Rudolf wants to shorten the string s by removing some characters to make it beautiful.

The main character doesn't like to strain, so he asks you to make the string beautiful by removing the minimum number of characters. He can remove characters from any positions in the string (not just from the beginning or end of the string).

†† String a is a substring of b if there exists a consecutive segment of characters in string b equal to a .

输入格式

The first line contains a single integer t ( 1≤t≤104 ) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains a single integer n ( 1≤n≤106 ) — the length of the string s .

The next line of each test case contains the string s of length n . The string s consists of lowercase Latin letters.

The sum of n over all test cases does not exceed 106106 .

输出格式

For each test case, output a single integer — the minimum number of characters that need to be deleted to make the string s beautiful. If the string is initially beautiful, then output 00 .

输入

输入 

6
9
mmapnapie
9
azabazapi
8
mappppie
18
mapmapmapmapmapmap
1
p
11
pppiepieeee

输出 

2
0
2
6
0
2

说明/提示

此题一点不复杂,直接统计map和pie的个数就行,注意一点就是没遇到一个map或pie则需要跳3,否则跳1,直接看代码,你就会明白;

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        string s;
        cin >> n >> s;
        
        int Count = 0;//计时器计入答案
        for (int i = 0; i < n; ) {
            if (i + 2 < n && ((s[i] == 'p' && s[i+1] == 'i' && s[i+2] == 'e') || (s[i] == 'm' && s[i+1] == 'a' && s[i+2] == 'p'))) {
                Count++; 
                i += 3; //防止重复计算,所以跳3
            } else {
                i++;
            }
        }
        
        cout <<Count << endl; 
    }
    return 0;
}

  • 30
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值