Educational Codeforces Round 71 (Rated for Div. 2)

A. There Are Two Types Of Burgers

水题。题意:给你面包片数,两种不同的肉饼数(制作一个汉堡要消耗两片面包和一片肉饼),之后再给你两种不同的汉堡售价,问你如何取得最大收益。

思路:照题意模拟即可 ,简单贪心。

#include <iostream>
#include <sstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int b, p, f;
        int h, c;
        cin >> b >> p >> f;
        cin >> h >> c;
        if (h > c)
        {
            int sum = 0;
            while (b >= 2 && p)
                sum += h, b -= 2, p--;
            while (b >= 2 && f)
                sum += c, b -= 2, f--;
            cout << sum << endl;
        }
        else
        {
            int sum = 0;
            while (b >= 2 && f)
                sum += c, b -= 2, f--;
            while (b >= 2 && p)
                sum += h, b -= 2, p--;
            cout << sum << endl;
        }
    }
}

B. Square Filling

算是水题吧。但是当时我想不出来(我还是太菜了wwww),还去问了子巨(子巨tql)。

暴力枚举判断即可。

题意:你有一个0矩阵。问你是否能经过操作变成题目给你的矩阵。

思路:枚举矩阵的每一个点。之后依次turn(将周围变成1),之后check是否与给出矩阵相符。若不符则turn_back,符合则压入vector。

注意的是可能存在都为0的情况,所以在最开始读入矩阵时,要加入flag判断是否题目给出的是0矩阵。

#include <iostream>
#include <sstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
int n, m;
int cnt;
int sample[51][51];
int temp[51][51];
int tmp[2][2];
vector <int> ans;
int final_check(){
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (temp[i][j] != sample[i][j]) return 1;
    return 0;
}
void turn_back(int x, int y){
    for (int i = x; i <= x + 1; i++)
        for (int j = y; j <= y + 1; j++)
            temp[i][j] = tmp[i-x][j-y];
}

void turn(int x, int y){
    for (int i = 0; i <=  1; i++)
        for (int j = 0; j <= 1; j++)
            tmp[i][j] = temp[x+i][y+j];

    for (int i = x; i <= x + 1; i++){
        for (int j = y; j <= y + 1; j++){
            temp[i][j] = 1;
            if (temp[i][j] != sample[i][j]){
                turn_back(x, y);
                return ;
            }
        }
    }
    cnt++; ans.push_back(x); ans.push_back(y);
}

int main()
{
    cin >> n >> m;
    int flag = 0; //判断是否为0矩阵
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++){
            scanf("%d", &sample[i][j]);
            if (sample[i][j]) flag = 1;
        }
    for (int i = 0; i < n-1; i++) for (int j = 0; j < m-1; j++) turn(i, j);

    if (!flag) cout << "0" << endl;
    else if (!cnt || final_check()) cout << "-1" << endl;
    else{
        cout << cnt << endl;
        for (int i = 0; i < ans.size(); i+=2)
            cout << ans[i]+1 << " " << ans[i+1]+1 << endl;
    }
    return 0;
}

C. Gas Pipeline(补题)

题意:二进制01串。为1代表这个地方得是高度为2的柱子,为0代表可以为高度为1或者2的柱子。

起点和终点柱子高度都为1。同时横向走的时候需要管道。柱子的单位花费是b,管道的单位花费是a。问怎么安排使得花费最小

思路:这道题有两种思路贪心与DP。

dp:读题不难发现,对于每个地方,要么是低位,要么是高位,影响因素只有花费和1的位置。

易得dp转移方程为  1.dp[i][0]=min(dp[i-1][1]+2*a+b,dp[i-1][0]+a+b);
          2.dp[i][1]=min(dp[i-1][1]+a+2*b,dp[i-1][0]+2*a+2*b);
DP的代码:

#include <cstring>
#include <algorithm>
#include <cstdio>
typedef long long ll;
using namespace std;
#define maxn 200000 + 100
char s[maxn];
ll dp[maxn][2];
int main()
{
    ll n, a, b, t;
    scanf("%lld", &t);
    while (t--){
        scanf("%lld %lld %lld %s", &n, &a, &b, s);
        memset(dp, 0x3f, sizeof(dp));
        dp[0][0] = b; //一开始必然是低位
        for (ll i = 1; i <= n; i++){
            if ((s[i] == ‘0‘ && s[i-1] == ‘0‘) || i == n)
                dp[i][0] = min(dp[i-1][1] + 2 * a + b, dp[i-1][0] + a + b);
            dp[i][1] = min(dp[i-1][1] + a + 2 * b, dp[i-1][0] + 2 * a + 2 * b);
        }
        printf("%lld\n", dp[n][0]);
    }
}

贪心:如果有n个点的话,就会有n+1个柱子。字符串为1的话,1这个格子代表的两个点都必须是是高柱子。

首先确定基础花费:n个管道:n*a,n+1个低柱子:n*b,2个转折管道:2*a。中间可以高柱子的部分,假设长度为x,选择高柱子,额外花费为x*b。选择低柱子,花费为2*a。选择最小的就可以了。

贪心代码:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#define  maxn  200000 + 10
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;

ll rec_pilar[maxn];
char s[maxn];
int main(){
    ll t; scanf("%lld", &t);
    while (t--){
        memset(rec_pilar, 0, sizeof(rec_pilar));
        ll n, a, b;
        scanf("%lld %lld %lld", &n, &a, &b);
        scanf("%s", s);
        //st和ed是用来记录高位的最左边和最右边
        ll st = inf, ed = 0;
        for (ll i = 0; i < n; i++){
            //若s为1的话那么1左右两边的柱子都应该是高位
            if (s[i] == ‘1‘){
                rec_pilar[i] = 1;
                rec_pilar[i+1] = 1;
                st = min(st, i);
                ed = max(ed, i + 1);
            }
        }

        ll num0 = 0,num1 = 0; //num0记录低位,num1记录高位
        ll ans = 0;
        for (ll i = st; i <= ed; i++){
            ll tmp = 0;
            if (rec_pilar[i] == 0) num0++;
            else{
                num1++;
                //每次加上贪心结果
                ans += min(num0 * b, 2 * a);
                num0 = 0;
            }
        }

        ans += 2 * a + n * a + (n + 1) * b + num1 * b;
        //要是一直都是低位
        if (st == inf && ed == 0) ans -= 2 * a;
        printf("%lld\n", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Vikyanite/p/11427193.html

时间: 2024-09-29 21:29:14

Educational Codeforces Round 71 (Rated for Div. 2)的相关文章

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)

题意:https://codeforc.es/contest/1207/problem/D n个元素,每个元素有a.b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时good). 思路: 真难则反+容斥,反向考虑,ans1=如果a序列非递减则有a中各个数字出现次数的阶乘的乘积个,ans2=b序列也是一样. ans3=然后还要减去a序列和b序列都是good的方案数,就是元素相同的出现次数阶乘的乘积(注意,如果不存在双good就不算ans3). ANS就是:全排列 -

E. XOR Guessing 交互题 Educational Codeforces Round 71 (Rated for Div. 2)

E. XOR Guessing 交互题. 因为这个数最多只有14位 0~13,所以我们可以先处理后面7位,然后再处理后面7位. 因为异或的性质,如果一个数和0异或,那么就等于本身. 所以我们第一次异或1~100 所以 后面从7到13位就都是0,所以结果的后面的7位就可以算出来. 然后同理可以把前面七位找到. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm>

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm