week10-限时大模拟

本文解析了两道算法竞赛题目,包括一题关于长方体颜色分配的数学问题和一题涉及团队成员时间表安排的复杂逻辑问题。针对颜色分配问题,文章提供了C++代码实现,讨论了奇数和偶数边长条件下红色与蓝色正方体数量差值的最小化策略。对于时间表安排问题,提出了使用数组和映射数据结构进行时间点统计的方法,以确定所有团队成员可同时参加的会议时段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - 签到题

问题描述

有一个A×B×C的长方体。这个长方体是由A×B×C个1×1×1的小正方体组成的。
现在想给每个小正方体涂上颜色。
需要满以下三点条件:
(1)每个小正方体要么涂成红色,要么涂成蓝色。
(2)所有红色的小正方体组成一个长方体。
(3)所有蓝色的小正方体组成一个长方体。
现在求红色小正方体的数量和蓝色小正方体的数量的差异。

你需要找到红色正方体的数量与蓝色正方体的数量差值的绝对值的最小值。即min{|红色正方体数量 - 蓝色正方体数量|}。

Input

输入仅一行,三个数A B C (2≤A,B,C≤10^9)

Output

输出一个数字。
即差值绝对值的最小值。

Example

Simple Input 1

3 3 3

Simple Output 1

9

Simple Input 2

2 2 4

Simple Output 2

0

Simple Input 3

5 3 5

Simple Output 3

15

解题思路

因为要求求出红色正方体的数量与蓝色正方体的数量差值的绝对值的最小值。当长方体a, b, c中有一条边为偶数边,那么差值就为0;当长方体a, b, c中全为奇数边,那么差值就为较小为的两个奇数的积。

代码

#include <iostream>
#include <stdio.h> 
#include <algorithm>
using namespace std;

long long int a[3];

int main()
{
	int max1 = 0, sum = 0, result = 0;
	for(int i=0;i<3;i++)
	{
		scanf("%lld", &a[i]);
		if(a[i]%2==0)
		{
			printf("0\n");
			return 0;
		}		
	}	
	sort(a, a+3);
	long long int ans = a[0]*a[1];
    printf("%lld\n", ans);
	return 0;
}

B - 团 队 聚 会

问题描述

TA团队每周都会有很多任务,有的可以单独完成,有的则需要所有人聚到一起,开过会之后才能去做。但TA团队的每个成员都有各自的事情,找到所有人都有空的时间段并不是一件容易的事情。

给出每位助教的各项事情的时间表,你的任务是找出所有可以用来开会的时间段

Input

第一行一个数T(T≤100),表示数据组数。
对于每组数据,第一行一个数m(2 ≤ m ≤ 20),表示TA的数量。
对于每位TA,首先是一个数n(0≤ n≤100),表示该TA的任务数。接下来n行,表示各个任务的信息,格式如下:
YYYY MM DD hh mm ss YYYY MM DD hh mm ss “some string here”
每一行描述的信息为:开始时间的年、月、日、时、分、秒;结束时间的年、月、日、时、分、秒,以及一些字符串,描述任务的信息。

数据约定:
所有的数据信息均为固定位数,位数不足的在在前面补前导0,数据之间由空格隔开。
描述信息的字符串中间可能包含空格,且总长度不超过100。
所有的日期时间均在1800年1月1日00:00:00到2200年1月1日00:00:00之间。
为了简化问题,我们假定所有的月份(甚至2月)均是30天的,数据保证不含有不合法的日期。

注意每件事务的结束时间点也即是该成员可以开始参与开会的时间点。

Output

对于每一组数据,首先输出一行"Scenario #i:",i即表明是第i组数据。
接下来对于所有可以用来开会的时间段,每一个时间段输出一行。

需要满足如下规则:
1.在该时间段的任何时间点,都应该有至少两人在场。
2.在该时间段的任何时间点,至多有一位成员缺席。
3.该时间段的时间长度至少应该1h。

所有的成员都乐意一天24h进行工作。
举个例子,假如现在TA团队有3位成员,TT、zjm、hrz。
那么这样的时间段是合法的:会议开始之初只有TT和zjm,后来hrz加入了,hrz加入之后TT离开了,此后直到会议结束,hrz和zjm一直在场。

要求:
1.输出满足条件的所有的时间段,尽管某一段可能有400年那么长。
2.时间点的格式为MM/DD/YYYY hh:mm:ss。
3.时间段的输出格式为"appointment possible from T0 to T1",其中T0和T1均应满足时间点的格式。
4.严格按照格式进行匹配,如果长度不够则在前面补前导0。
5.按时间的先后顺序输出各个时间段。
6.如果没有合适的时间段,输出一行"no appointment possible"。
7.每组数据末尾须打印额外的一行空行。

Example

Simple Input

2
3
3
2020 06 28 15 00 00 2020 06 28 18 00 00 TT study
2020 06 29 10 00 00 2020 06 29 15 00 00 TT solving problems
2020 11 15 15 00 00 2020 11 17 23 00 00 TT play with his magic cat
4
2020 06 25 13 30 00 2020 06 25 15 30 00 hrz play
2020 06 26 13 30 00 2020 06 26 15 30 00 hrz study
2020 06 29 13 00 00 2020 06 29 15 00 00 hrz debug
2020 06 30 13 00 00 2020 06 30 15 00 00 hrz play
1
2020 06 01 00 00 00 2020 06 29 18 00 00 zjm study
2
1
1800 01 01 00 00 00 2200 01 01 00 00 00 sleep
0

Simple Output

Scenario #1:
appointment possible from 01/01/1800 00:00:00 to 06/25/2020 13:30:00
appointment possible from 06/25/2020 15:30:00 to 06/26/2020 13:30:00
appointment possible from 06/26/2020 15:30:00 to 06/28/2020 15:00:00
appointment possible from 06/28/2020 18:00:00 to 06/29/2020 10:00:00
appointment possible from 06/29/2020 15:00:00 to 01/01/2200 00:00:00

Scenario #2:
no appointment possible

解题思路

因为要确定至多有一个TA不空闲的时间段,因此我们首先要做的就是将各个时间段转化成数组进行统计。首先我想到的是通过set和map来完成转化,但是这个会造成超时,应该使用vector和unordered_map才可以,但是后来发现其实不用unordered_也是可以的。

首先确定各个时间点的先后顺序(如果时间段长度为0,则不加入统计),然后对各个时间段进行统计,对除终点外其他点都进行加1操作。然后即可获得一个代表各时间点TA的忙碌人数的数组。然后进行统计,为了便于统计起始设为最小的时间点(1800),终点设为(2200),终点的忙碌人数设为MAX。然后顺次遍历数组,每当遇到忙碌人数为2的时间点,就将其作为终点,输出起点和终点之间的时间段。然后更新起点为当前时间点的下一个时间点。重复上述过程即可完成各个时间段的输出。

代码1

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;

struct time{
	int a, b, c, d, e, f;
	int count;
	time(){}
	bool operator <(const time& t) const{
		if(a!=t.a)
			return a<t.a;
		else if(b!=t.b)
			return b<t.b;
		else if(c!=t.c)
			return c<t.c;
		else if(d!=t.d)
			return d<t.d;
		else if(e!=t.e)
			return e<t.e;
		else if(f!=t.f)
			return f<t.f;
	}
	bool operator == (const time & x)
	{ 
		if (a==x.a && b==x.b && c==x.c && d==x.d && e==x.e && f==x.f)
			return true;
		return false;
	}
};
bool gettime(time l,time r)
{
	if(r.a-l.a>=2)
		return true;
	r.b+=(r.a-l.a)*12;
	if(r.b-l.b>=2)
		return true;
	r.c+=(r.b-l.b)*30;
	if(r.c-l.c>=2)
		return true;
	r.d+=(r.c-l.c)*24;
	if(r.d-l.d>=2)
		return true;
	r.e+=(r.d-l.d)*60;
	r.f+=(r.e-l.e)*60;
	if(r.f-l.f>=3600)
		return true;
	return false;
}
void print2(time t1, time t2){
	printf("appointment possible from %02d/%02d/%02d %02d:%02d:%02d", t1.b, t1.c, t1.a, t1.d, t1.e, t1.f); 
	printf("to %02d/%02d/%02d %02d:%02d:%02d\n", t2.b, t2.c, t2.a, t2.d, t2.e, t2.f);
}

vector<time> a;
int b[10000], d[10000];
bool status;//记录是否有空闲时间 

int main()
{
	int t, m, n, num;
	string s;
	time t1, t2;
	scanf("%d", &t);
	for(int i=0;i<t;i++)
	{
		a.clear();
		memset(b, 0, sizeof(b));
		memset(d, 0, sizeof(d));
		status = false; 
		scanf("%d", &n);
		int tot = 0;
		for(int i=0;i<n;i++)
		{
			scanf("%d", &m);
			for(int i=0;i<m;i++)
			{
				scanf("%d %d %d %d %d %d", &t1.a, &t1.b, &t1.c, &t1.d, &t1.e, &t1.f);
				t1.count = tot++;
				scanf("%d %d %d %d %d %d", &t2.a, &t2.b, &t2.c, &t2.d, &t2.e, &t2.f);
				t2.count = tot++;
				getline(cin, s, '\n');
				if(t1==t2)
					tot = tot - 2;
				else
					a.push_back(t1), a.push_back(t2);
			}
		}
		sort(a.begin(), a.end());
		for(int i=0;i<a.size();i++)
			b[a[i].count] = i;	
		for (int i=0;i<tot/2;i++)
			for (int j=b[i*2];j<b[i*2+1];j++)
				d[j]++;
		t1.a = 1800, t1.b = 1, t1.c = 1, t1.d = 0, t1.e = 0, t1.f = 0, t1.count = 0;
		t2.a = 2200, t2.b = 1, t2.c = 1, t2.d = 0, t2.e = 0, t2.f = 0, t2.count = 0;
		a.push_back(t2);
		d[tot] = 10000;
		printf("Scenario #%d:\n", i+1);
		num = 1;
		if(n==2)
			num = 0;
		for(int i=0;i<=tot;i++)
		{
			if(d[i]>num)
			{
				if(gettime(t1, a[i]))
				{
					status = true; 
					print2(t1, a[i]);
				}
				t1 = a[i+1];
			}	
		}
		if(!status)	
			printf("no appointment possible\n");
		printf("\n");
	}
}

代码2

#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <set>
#include <string>
using namespace std;

struct time{
	int a, b, c, d, e, f;
	time(){}
	bool operator <(const time& t) const{
		if(a!=t.a)
			return a<t.a;
		else if(b!=t.b)
			return b<t.b;
		else if(c!=t.c)
			return c<t.c;
		else if(d!=t.d)
			return d<t.d;
		else if(e!=t.e)
			return e<t.e;
		else
			return f<t.f;
	}
};
bool gettime(time l,time r)
{
	if(r.a-l.a>=2)
		return true;
	r.b+=(r.a-l.a)*12;
	if(r.b-l.b>=2)
		return true;
	r.c+=(r.b-l.b)*30;
	if(r.c-l.c>=2)
		return true;
	r.d+=(r.c-l.c)*24;
	if(r.d-l.d>=2)
		return true;
	r.e+=(r.d-l.d)*60;
	r.f+=(r.e-l.e)*60;
	if(r.f-l.f>=3600)
		return true;
	return false;
}
void print2(time t1, time t2){
	printf("appointment possible from %02d/%02d/%02d %02d:%02d:%02d", t1.b, t1.c, t1.a ,t1.d, t1.e, t1.f); 
	printf(" to %02d/%02d/%02d %02d:%02d:%02d\n", t2.b, t2.c, t2.a, t2.d, t2.e, t2.f);
}

set<time> a;
map<time , int> b;
map<int , time> b1;
vector<time> c;
bool status;//记录是否有空闲时间 

int count[10000];

int main()
{
	int t, m, n;
	string s;
	time t1, t2;
	scanf("%d", &t);
	for(int i=0;i<t;i++)
	{
		a.clear(), b.clear(), b1.clear(), c.clear();
		status = false; 
		scanf("%d", &n);
		for(int i=0;i<n;i++)
		{
			scanf("%d", &m);
			for(int i=0;i<m;i++)
			{
				scanf("%d %d %d %d %d %d", &t1.a, &t1.b, &t1.c, &t1.d, &t1.e, &t1.f);
				scanf("%d %d %d %d %d %d", &t2.a, &t2.b, &t2.c, &t2.d, &t2.e, &t2.f);
				getline(cin, s, '\n');
				a.insert(t1), a.insert(t2);
				c.push_back(t1), c.push_back(t2);
			}
		}
		t1.a = 1800, t1.b = 1, t1.c = 1, t1.d = 0, t1.e = 0, t1.f = 0;
		t2.a = 2200, t2.b = 1, t2.c = 1, t2.d = 0, t2.e = 0, t2.f = 0;
		a.insert(t1), a.insert(t2);
		for(int i=0;i<a.size();i++) 
			count[i]=0;
		int j=0, count1 = 0;
		for(set<time>::iterator it = a.begin();it!=a.end();it++, j++)	
		{
			b[*it] = j;
			b1[j] = *it;
		}
		for(int i=0;i<c.size();i++)
		{
			int m = b[c[i]];
			i++;
			int n = b[c[i]];
			for(int j=m;j<n;j++)
				count[j]++;
		}
		count[a.size()-1]=100000;
		printf("Scenario #%d:\n", i+1);
		int begin = 0, num = 1;
		if(n==2)
			num = 0;
		for(int i=0;i<a.size();i++)
		{
			if(count[i]>num)
			{
				if(gettime(b1[begin], b1[i]))
				{
					status = true; 
					print2(b1[begin], b1[i]);
				}
				begin = i+1;	
			}	
		}
		if(!status)	
			printf("no appointment possible\n");
		printf("\n");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值