Codeforces Round #784 (Div. 4) (A - H)题解

难得的一次Div4,从头写到尾

A. Division?

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define endl '\n'

using namespace std;

const int N = 1e5 + 10;

void mian()
{
	int n;
	cin >> n;
	if (n <= 1399)
		cout << "Division 4" << endl;
	else if (n <= 1599)
		cout << "Division 3" << endl;
	else if (n <= 1899)
		cout << "Division 2" << endl;
	else
		cout << "Division 1" << endl;
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		mian();
	}
}

B. Triple

找三个相同的数然后输出,没有就输出-1

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <unordered_map>

#define endl '\n'

using namespace std;

const int N = 1e5 + 10;

unordered_map<int, int> t;

void mian()
{
	int n;
	cin >> n;
	t.clear();
	bool flag = true;
	for (int i = 1; i <= n; i++)
	{
		int a;
		cin >> a;
		t[a]++;
		if (flag && t[a] >= 3)
		{
			cout << a << endl;
			flag = false;
		}
	}
	if (flag)
		cout << -1 << endl;
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		mian();
	}
}

C. Odd/Even Increments

可以给所有奇数位或偶数位加1,如果能最终把所有数都变成奇数或者偶数,就输出YES

判断当前所有奇数位的数的奇偶性是不是一样,然后判断所有偶数位

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define endl '\n'

using namespace std;

const int N = 1e5 + 10;

void mian()
{
	int n;
	cin >> n;
	int a, b, c;
	cin >> a >> b;
	bool flag = true;
	for (int i = 3; i <= n; i++)
	{
		cin >> c;
		if (i & 1)
		{
			if (a % 2 != c % 2)
				flag = false;
		}
		else
		{
			if (b % 2 != c % 2)
				flag = false;
		}
	}
	if (flag)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		mian();
	}
}

D. Colorful Stamp

可以给连续两张邮票染色,必须染不同的色,可以重复染,但是一次必须连续两张。

从头到尾扫描一遍,以w分割,每个子串中如果有R和B那就是合法的。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define endl '\n'

using namespace std;

const int N = 1e5 + 10;

void mian()
{
	int n;
	string s;
	cin >> n >> s;
	int b = 0, r = 0;
	bool flag = true;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'W')
		{
			if (b == 0 && r != 0 || b != 0 && r == 0)
				flag = false;
			b = 0, r = 0;
		}
		else if (s[i] == 'B')
		{
			b++;
		}
		else if (s[i] == 'R')
		{
			r++;
		}
	}
	if (b == 0 && r != 0 || b != 0 && r == 0)
		flag = false;
	if (flag)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		mian();
	}
}

E. 2-Letter Strings

题意:给n个长度为2字符串,找出多少对同一个位置字符相同,另一个位置字符不同的字符串。

思路:存一下第一个位置为x的字符中字符y的个数,然后对应相乘,再加到一起。然后第二个字符同理。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define endl '\n'

using namespace std;

typedef long long ll;

const int N = 30;

struct node
{
	vector<int> num;
	node()
	{
		num.resize(30, 0);
	}
};

void mian()
{
	node a[N], b[N];
	int n;
	cin >> n;
	string s;
	for (int i = 1; i <= n; i++)
	{
		cin >> s;
		a[s[0] - 'a'].num[s[1] - 'a']++;
		b[s[1] - 'a'].num[s[0] - 'a']++;
	}
	ll res = 0;
	for (int i = 0; i <= 'k' - 'a'; i++)
	{
		for (int j = 0; j <= 'k' - 'a'; j++)
		{
			for (int k = j + 1; k <= 'k' - 'a'; k++)
			{
				res += 1ll * a[i].num[j] * a[i].num[k];
			}
		}
	}
	for (int i = 0; i <= 'k' - 'a'; i++)
	{
		for (int j = 0; j <= 'k' - 'a'; j++)
		{
			for (int k = j + 1; k <= 'k' - 'a'; k++)
			{
				res += 1ll * b[i].num[j] * b[i].num[k];
			}
		}
	}
	cout << res << endl;
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		mian();
	}
}

F. Eating Candies

题意:两人分别从左边右边开始吃糖果,不能跳着吃,问他们想要吃的重量一样,他们一共要吃多少个。

双指针枚举即可

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define endl '\n'

using namespace std;

const int N = 1e5 + 10;

void mian()
{
    int n;
    cin >> n;
    vector<int> a(n + 1, 0);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    int res = 0, cnt = 0;
    int l = 1, r = n;
    int alice = 0, bob = 0;
    while (l <= r)
    {
        if (alice == bob)
        {
            res = cnt;
            alice += a[l++];
            cnt++;
        }
        else if (alice > bob)
        {
            bob += a[r--];
            cnt++;
        }
        else
        {
            alice += a[l++];
            cnt++;
        }
    }
    if (alice == bob)
    {
        res = cnt;
    }
    cout << res << endl;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        mian();
    }
}

G. Fall Down

有三种东西,.是空气*是石头,o不知道是啥,反正不能掉下来,石头能掉下来,掉到石头上或者o上。

一层一层跑,如果跑一遍没变化就是跑完了,可以退出了。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define endl '\n'

using namespace std;

const int N = 60;

void mian()
{
    int n, m;
    string a[N];
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        a[i] = " " + a[i];
    }
    bool flag = true;
    while (flag)
    {
        flag = false;
        for (int i = n; i > 1; i--)
        {
            for (int j = 1; j <= m; j++)
            {
                if (a[i][j] == '.' && a[i - 1][j] == '*')
                {
                    a[i][j] = '*';
                    a[i - 1][j] = '.';
                    flag = true;
                }
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        cout << a[i] << endl;
    }
    cout << endl;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        mian();
    }
}

H. Maximal AND

就是可以给数组中一个数的二进制任意一位变成1,求这一堆数求与最大能变为多少。可以变k次。

存一下所有数的二进制位的每一位到一个数组,如果数组中的那一个达到n了,说明最终的这一位是1,然后k次就贪心的添加,如果全加进去不能到n,那就换低一位的。

样例解释:

3 2
2 1 1

有3个数 2次转机会
转换为二进制

10
01
01

三个数求与运算
可以把两个1的二进制转换为11
刚好可以把两个1转换成3

10
11
11

求与运算 3&2&2 = 2

可以发现所有数的那一位都是1的话最终结果的那一位才会是1
想要最终结果最大的话就需要尽量让最高位为1
那么就贪心的判断就行
如果所有机会都给最高位都不行那就换低一位的数

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define endl '\n'

using namespace std;

const int N = 1e5 + 10;

void mian()
{
    int a[40] = {0};
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        int t;
        cin >> t;
        int idx = 0;
        while (t)
        {
            a[idx++] += (t & 1);
            t >>= 1;
        }
    }
    for (int i = 30; i >= 0; i--)
    {
        if (a[i] + k >= n)
        {
            k -= n - a[i];
            a[i] = n;
        }
        if (k <= 0)
            break;
    }
    int res = 0;
    int t = 1;
    for (int i = 0; i <= 30; i++)
    {
        if (a[i] == n)
            res += t;
        t <<= 1;
    }
    cout << res << endl;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        mian();
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值