AtCoder abc 355

https://atcoder.jp/contests/abc355

A:

问题陈述:

Takahashi’s cake has been eaten by someone. There are three suspects: person 1 1 1, person 2 2 2, and person 3 3 3.

There are two witnesses, Ringo and Snuke. Ringo remembers that person A A A is not the culprit, and Snuke remembers that person B B B is not the culprit.

Determine if the culprit can be uniquely identified based on the memories of the two witnesses. If the culprit can be identified, print the person’s number.

分析:

在1,2,3这三个数中,输入两个值,若这两个值相同,输出-1,若这两个数不相等,输出除这两个数之外的第三个数的值。(因为本题中1,2,3,是固定数值,因此我们也可以输出(6-a-b)或者输出a^b)

代码1:

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

map<int,int> s;
int main()
{
	int a,b;
	cin >> a;
	s[a]=1;
	cin >> b;
	s[b]=1;
	if(a==b)
	cout << -1;
	else
	for(int i=1;i<=3;i++)
	{
		if(s[i]!=1)
		cout << i ;
	}
	return 0;
}

代码2:

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

int main()
{
	int a,b;
	cin >> a >> b;
	if(a==b)
	cout << -1;
	else
	cout << 6-a-b;
    //cout << a^b;
	return 0;
}

B:

问题陈述

You are given a sequence A = ( A 1 , A 2 , … , A N ) A=(A_1,A_2,\dots,A_N) A=(A1,A2,,AN) of length N N N and a sequence B = ( B 1 , B 2 , … , B M ) B=(B_1,B_2,\dots,B_M) B=(B1,B2,,BM) of length M M M. Here, all elements of A A A and B B B are pairwise distinct. Determine whether the sequence C = ( C 1 , C 2 , … , C N + M ) C=(C_1,C_2,\dots,C_{N+M}) C=(C1,C2,,CN+M) formed by sorting all elements of A A A and B B B in ascending order contains two consecutive elements appearing in A A A.

分析:

输入A,B两个数组,分别对其进行排序,对aA,B数组循环,判断是否出现A中两个连续元素。

代码实现:

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

int a[110],b[110];

int main()
{
	int n,m;
	cin >> n >> m;
	for(int i=1;i<=n;i++)
	{
		cin >> a[i]; 
	}
	sort(a+1,a+1+n);
	for(int i=1;i<=m;i++)
	{
		cin >> b[i];
	}
	sort(b+1,b+m+1);
	int i=1,j=1,flag=0;
	while(i<=n&&j<=m)
	{
		if(a[i]<b[j])
		{
			i++;
			flag++;
			if(flag==2)
			{
				cout << "Yes";
				return 0;
			}
		}
		else
		{
			j++;
			flag=0;
		}
	}
	while(i<=n) {
		i++;
		flag++;
		if(flag==2)
		{
			cout << "Yes";
			return 0;
		}
	}
	cout << "No";
	return 0;
}

C:

问题描述:

There is an N × N N \times N N×N grid, where the cell at the i i i-th row from the top and the j j j-th column from the left contains the integer N × ( i − 1 ) + j N \times (i-1) + j N×(i1)+j.

Over T T T turns, integers will be announced. On Turn i i i, the integer A i A_i Ai is announced, and the cell containing A i A_i Ai is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within T T T turns, print -1.

Here, achieving Bingo means satisfying at least one of the following conditions:

  • There exists a row in which all N N N cells are marked.
  • There exists a column in which all N N N cells are marked.
  • There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all N N N cells are marked.

分析:

首先,我们需要按照输入数据构造一个N*N的表格,表格中的数据为1-N,分别定义行数组,列数组,和对应的两个对角线数组,初定义均为零。

读入每个回合输入的值对应的每行每列,及对角线数组的值加一,判断是否有数组的值达到N,如果有,则输出此时的 i 值,直接返回,否则继续进行,直到输出-1。

代码实现:

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

const int N = 2*1e3+10;
int a[N][N];
int t[N*N];
int r[N]={0},l[N]={0},c[N]={0};
int main()
{
	int n,T;
	cin >> n >> T;
	for(int i=1,k=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++) 
		a[i][j]=k++;
	}
	for(int i=1;i<=T;i++)
	{
		cin >> t[i];
        t[i]--;//对数据进行处理
		int x=t[i]/n;
		int y=t[i]%n; //判断行和列
		if(x==y) c[1]++; 
		if(x+y==n-1) c[2]++; //判断是否在对角线上
		if(++r[x]==n||++l[y]==n||c[1]==n||c[2]==n)
		{
			cout << i;
			return 0;
		}
	} 
	cout << -1;
	return 0;
}

D:

问题描述:

You are given N N N intervals of real numbers. The i i i-th ( 1 ≤ i ≤ N ) (1 \leq i \leq N) (1iN) interval is [ l i , r i ] [l_i, r_i] [li,ri]. Find the number of pairs ( i , j )   ( 1 ≤ i < j ≤ N ) (i, j)\,(1 \leq i < j \leq N) (i,j)(1i<jN) such that the i i i-th and j j j-th intervals intersect.

分析:

本题中我们要找出有相交部分的区间对数,根据数据范围来看,暴力循环显然不可行,要找出有相交部分的区间对比较麻烦,所以,我们可以选择找到不相交区间的对数再用总区间对数减去该值。
要找到不相交的区间堆,我们可以分别对左区间和右区间进行排序,然后将利用双指针遍历,计算出不相交区间对的数量。

代码实现:

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

const int N = 5*1e5+10;
int l[N],r[N];

int main()
{
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> l[i] >> r[i];
    }
    sort(l,l+n);
    sort(r,r+n);
    long long int ans=(long long)(n-1)*n/2; //一定要开long long 
    for(int i=0,j=0;i<n;i++)
    {
        while(j<n&&r[j]<l[i])
        {
            j++;
        }
        ans-=j;
    }
    cout << ans ;
    return 0;
}

24/5/27

  • 31
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值