Codeforces Round 886 (Div. 4)(A~G)

在这里插入图片描述

Codeforces Round 886 (Div. 4)(A~G)

[题目链接](Dashboard - Codeforces Round 886 (Div. 4) - Codeforces)

A. To My Critics

思路:仨数挑俩大的跟10比较-_-

AC代码:

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

const int N = 5;
int a[N];
int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		for(int i = 0; i < 3; i ++)
		cin >> a[i];
		
		sort(a, a + 3);
		
		if(a[2] + a[1] >= 10)
		cout << "YES" << endl;
		else
		cout << "NO" << endl;	
	}
	return 0;
}

B. Ten Words of Wisdom

思路:输入限制下,在<=10的字数中找出质量最大的即可-_-

AC代码:

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

const int N = 10001;
int a[N], b[N];

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		int n;
		cin >> n;
		for(int i = 0; i < n; i ++)
		{
			cin >> a[i] >> b[i];
		}
		
		int ma = 0, ans = 0;
		for(int i = 0; i < n; i ++)
		{
			if(a[i] <= 10 && b[i] > ma)
			{
				ma = b[i];
				ans = i;
			}
		}
		cout << ans + 1 << endl;
	}
	return 0;
}

C. Word on the Paper

思路:找不是’.'的字符输出-_-

AC代码:

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

char g[10][10];

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		for(int i = 0; i < 8; i ++)
		for(int j = 0; j < 8; j ++)
		{
			cin >> g[i][j];
		}
		
		for(int i = 0; i < 8; i ++)
		for(int j = 0; j < 8; j ++)
		{
			if(g[i][j] != '.')
			cout << g[i][j];
		}
		cout << endl;
	}
	
	return 0;
}

D. Balanced Round

题意:删除给定序列的一些元素(可为0),使其可以按一种顺序令相邻元素绝对值<=k,找出达成条件的最小删除数。

思路:排序序列,寻找最长相邻元素绝对值为k的子序列,最小删除数即序列长度减去最长子序列。

AC代码:

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

const int N = 200100;
int q[N], a[N];

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		int n, k;
		cin >> n >> k;
		for(int i = 0; i < n; i ++)
		{
			cin >> q[i];
			a[i] = q[i];
		}
		
		if(n == 1)
		cout << 0 << endl;
		else if(n == 2)
		{
			if(abs(q[0] - q[1]) > k)
			cout << 1 << endl;
			else
			cout << 0 << endl;
		}
		else
		{
			sort(a, a + n);
			
			int ma = 1000000, res = 0, cnt = 1;
			for(int i = 0; i < n - 1; i ++)
			{
				if(a[i + 1] - a[i] <= k)
				{
					cnt ++;
				}
				else
				{
					if(cnt > res)
					res = cnt;
					
					cnt = 1;
				}
			}
			res = max(cnt, res);
			cout << n - res << endl;
		}	
	}
	return 0;
}

E. Cardboard for Pictures

题意:数学题,n张正方形图片,每张图片需要一张比边长多w的硬纸板边框,给出每张图片尺寸和所用硬纸板总面积,求w的值。

思路:

  1. 图片边长为a,硬纸板边框为(a + 2w),可得硬纸板面积 - 图片面积 = 超出图片面积(2*(a *w+(a+w) *w));

  2. 整理得一元二次方程,运用求根公式,得出结果;

  3. 注意,由于运算过程中数值过大,会超出C++的longlong,需_int128或longdouble,最后结果需要将浮点数形式转换为整型。

AC代码:

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

typedef long double LD;
typedef long long LL;
const int N = 200100;
int q[N];

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		LD n, k, sn = 0, sn2 = 0;
		cin >> n >> k;
		for(int i = 0; i < n; i ++)
		{
			cin >> q[i];
			sn += q[i];
			sn2 += (q[i] * q[i]);
		}
	
		LD a = 0, b = 0, c = 0, res1 = 0, res2 = 0, cnt1 = 0, cnt2 = 0;
		a = 4 * n;
		b = 4 * sn;
		c =  sn2 - k;
		cnt1 = (-b) + (sqrt(b * b - 4 * a * c));
		cnt2 = (-b) - (sqrt(b * b - 4 * a * c));
		
		res1 = cnt1 / (2 * a);
		res2 = cnt2 / (2 * a);
		
		LL ans = 0;
		
		if(res1 > 0)
		ans = res1;
		else
		ans = res2;
		
		cout << ans << endl;
	}
	
	return 0;
}

F. We Were Both Children

题意:给定n只青蛙每次跳跃的距离,在距离n以内放置一处陷阱,询问可以捕捉到最多青蛙的数量。

思路:从小到大排序,记录n以内每只青蛙所能跳跃到的各个点,最后遍历n以内的各个点查询被记录最多的点,即为答案。

核心操作:

int tt = 1;
		for(int i = 1; i <= n; i ++)
		{
			if(i + 1 <= n && q[i] == q[i + 1])
			{
				tt ++;//记录跳跃能力相同的娃
				continue;
			}
			for(int j = 1; j <= n / q[i]; j ++)
			cnt[j * q[i]] += tt;//记录当前青蛙在n以内可跳跃到的地点
			tt = 1;
		}

AC代码:

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

const int N = 200100;
int q[N], cnt[N];

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		memset(cnt, 0, sizeof cnt);
		
		int n;
		cin >> n;
		for(int i = 1; i <= n; i ++)
		cin >> q[i];
		
		sort(q + 1, q + n + 1);
		
		int tt = 1;
		for(int i = 1; i <= n; i ++)
		{
			if(i + 1 <= n && q[i] == q[i + 1])
			{
				tt ++;
				continue;
			}
			for(int j = 1; j <= n / q[i]; j ++)
			cnt[j * q[i]] += tt;
			
			tt = 1;
		}
		int ans = 0;
		for(int i = 1; i <= n; i ++)
		ans = max(ans, cnt[i]);
		
		cout << ans << endl;
	}
	
	return 0;
}

G. The Morning Star

题意:给定n个坐标,寻找最多有多少对坐标在同一行/列/对角线

思路:

  1. 判断两坐标是否在同一直线,行/列只要x/y坐标其一相同即两坐标在同一行/列;根据题意,对角线直线斜率必为1或-1,则直线方程y = x + c / y = -x + c ,判断x+y或x-y 是否相等即可。

  2. 题目数值较大,需用map来分别记录同一行/列/对角线的坐标数量mp[i],之后遍历mp,结果为mp[i] * (mp[i] - 1)的和。

    遍历方式:

    for(auto i:mp)
    ans += i.second * (i.second - 1);
    
  3. 也可不遍历,在存取map的过程中,若一坐标在同一直线的条件>1次达成,则判断该坐标与先前出现的

    有同一直线条件的坐标可成一组,最终结果*2。[参考知乎大佬](Codeforces Round 886 (Div. 4) A - H - 知乎 (zhihu.com))

AC代码:

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

typedef long long LL;
typedef pair<int, int> PII;
const int N = 200100;
PII q[N];

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		map<int, int> m1, m2, m3, m4;
		LL n, ans = 0;
		cin >> n;
		for(int i = 0; i < n; i ++)
		{
			cin >> q[i].first >> q[i].second;
			int a = q[i].first, b = q[i].second;
			
			ans += m1[a];
			ans += m2[b];
			ans += m3[a - b];
			ans += m4[a + b];
			
			m1[a] ++;
			m2[b] ++;
			m3[a - b] ++;
			m4[a + b] ++;
 		}
		
		cout << ans * 2 << endl;
	}
	
	return 0;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
const int N = 200100;
PII q[N];

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		map<int, int> m1, m2, m3, m4;
		int n;
		cin >> n;
		for(int i = 0; i < n; i ++)
		{
			cin >> q[i].first >> q[i].second;
			int a = q[i].first, b = q[i].second;

			m1[a] ++;
			m2[b] ++;
			m3[a - b] ++;
			m4[a + b] ++;
 		}
		
		LL ans = 0;
		for(auto i:m1)
		ans += i.second * (i.second - 1);
		for(auto i:m2)
		ans += i.second * (i.second - 1);
		for(auto i:m3)
		ans += i.second * (i.second - 1);
		for(auto i:m4)
		ans += i.second * (i.second - 1);
		
		cout << ans << endl;
	}
	
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值