Week10 - 程序设计思维与实践 - 限时大模拟(Time Planner)

A-签到题

题意

TT有一个A×B×C的长方体。这个长方体是由A×B×C个1×1×1的小正方体组成的。

现在TT想给每个小正方体涂上颜色。

需要满以下三点条件:

  • 每个小正方体要么涂成红色,要么涂成蓝色。
  • 所有红色的小正方体组成一个长方体。
  • 所有蓝色的小正方体组成一个长方体。

现在TT想知道红色小正方体的数量和蓝色小正方体的数量的差异。

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

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll a,b,c,red,blue;
vector<ll> s;

int main()
{
	cin>>a>>b>>c;
	red=a/2,blue=a-a/2;
	s.push_back(abs(red*b*c-blue*b*c));
	red=b/2,blue=b-b/2;
	s.push_back(abs(red*a*c-blue*a*c));
	red=c/2,blue=c-c/2;
	s.push_back(abs(red*a*b-blue*a*b));
	sort(s.begin(),s.end());
	cout<<s[0]<<endl;
	return 0;
}

B POJ1960 - Time Planner

Background

Problems in computer science are often classified as belonging to a certain class of problems (e.g. NP, NP complete, unsolvable, . . . ). Whatever class of problems you have to solve in a team, there is always one main problem: To find a date where all the programmers can meet to work together on their project.

Problem

Your task is to write a program that finds all possible appointments for a programming team that is busy all the time and therefore has problems in finding accurate dates and times for their meetings.

Input

The first line of the input contains the number of scenarios.
For each scenario, you are first given the number m of team members in a single line, with 2 <= m <= 20.For every team member, there will be a line containing the number n of entries in this members calender (0 <= n <= 100), followed by n lines in the following format:

YYYY MM DD hh mm ss YYYY MM DD hh mm ss some string here

Each such line contains the year, month, day, hour, minute, and second of both starting and ending time for the appointment, and a string with the description of the appointment. All numbers are zero padded to match the given format, with single blanks in-between. The string at the end might contain blanks and is no longer than 100 characters. The dates are in the range January 1, 1800 midnight to January 1, 2200 midnight. For simplification, you may assume that all months (even February) have 30 days each, and no invalid dates (like January 31) will occur.
Note that the ending time of a team member’s appointment specifies the point in time where this team member is ready to join a meeting.

Output

The output for each scenario begins with a line containing Scenario #i:, where i is the number of the scenario starting at 1. Then print a line for each possible appointments that follows these rules:
at any time during the meeting, at least two team members need to be there
at any time during the meeting, at most one team member is allowed to be absent
the meeting should be at least one hour in length
all team members are willing to work 24 hours a day

For example, if there are three team members A, B and C, the following is a valid meeting: It begins solely with A and B. Later C joins the meeting and before the end, A may leave.
Always print the longest appointment possible satisfying these conditions, even if it is as long as 400 years. Sort these lines by date and time, and use the following format:
appointment possible from MM/DD/YYYY hh:mm:ss to MM/DD/YYYY hh:mm:ss
The numbers indicating month, day, year, hour, minute, and second should be zero padded in order to get the required number of characters. If no appointment is possible, just print a line containing no appointment possible Conclude the output for each scenario with a blank line.

Sample Input

2
3
3
2002 06 28 15 00 00 2002 06 28 18 00 00 TUD Contest Practice Session
2002 06 29 10 00 00 2002 06 29 15 00 00 TUD Contest
2002 11 15 15 00 00 2002 11 17 23 00 00 NWERC Delft
4
2002 06 25 13 30 00 2002 06 25 15 30 00 FIFA World Cup Semifinal I
2002 06 26 13 30 00 2002 06 26 15 30 00 FIFA World Cup Semifinal II
2002 06 29 13 00 00 2002 06 29 15 00 00 FIFA World Cup Third Place
2002 06 30 13 00 00 2002 06 30 15 00 00 FIFA World Cup Final
1
2002 06 01 00 00 00 2002 06 29 18 00 00 Preparation of Problem Set
2
1
1800 01 01 00 00 00 2200 01 01 00 00 00 Solving Problem 8
0

Sample Output

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

Scenario #2:
no appointment possible

思路

  由题意得,一场会议的起止时间点肯定是所输入的数据(包括题意中所含的两个边界时间),那么将输入的所有时间点从小到大排序,利用滑动窗口的思想枚举终点和起点,尽量选取最长的时间区间,由于会议区间需满足三个条件,所以在枚举的时候进行检验,满足条件即输出。

  • Check_Period:将起止时间点换算为秒数,若时间间隔至少为 1h 则满足时间长度的要求
  • Check_Start: 遍历存储个人时间信息的数组,并更新对应的 pre 数组,统计符合条件(边界条件需特判)的人数,若人数至少为两人且缺席人数不多于一人则满足规则1与规则2的要求。
  • Check_End: 方法同上,不过要注意,起始时间可以与个人事务的终止时间相等,但不能与个人事务的起始时间相等,终止时间可以与个人事务的起始时间相等,但不能与个人事务的终止时间相等。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;

struct timetable{
	int year,month,day,hour,minute,seco;
	timetable(int yy=0,int mm=0,int dd=0,int hh=0,int mi=0,int ss=0)
		:year(yy),month(mm),day(dd),hour(hh),minute(mi),seco(ss){}
	bool operator<(const timetable &s)const{
		if(year!=s.year)	return year<s.year;
		if(month!=s.month)	return month<s.month;
		if(day!=s.day)		return day<s.day;
		if(hour!=s.hour)	return hour<s.hour;
		if(minute!=s.minute)	return minute<s.minute;
		return seco<s.seco; 
	}
	timetable& operator=(const timetable &s){
		year=s.year,month=s.month,day=s.day;
		hour=s.hour,minute=s.minute,seco=s.seco;
		return *this;
	}
	bool operator<=(const timetable &s)const{
		return !(s<*this);
	}
};

string tmp;
int T,m,n,tot,pre_l[30],pre_r[30],num[30];
timetable s[30][210],e[30][210],a[4010],start,end,zerotime;

void init(){
	tot=0;
	for(int i=0;i<=20;++i){	
		for(int j=0;j<num[i];++j)
			s[i][j]=e[i][j]=zerotime;
		pre_l[i]=pre_r[i]=num[i]=0;
	}
}

bool Check_Start(int l){
	int cnt=0;
	for(int i=0;i<m;++i){
		if(num[i]==0){
			cnt++;continue;
		}
		if(a[l]<s[i][0]||(e[i][num[i]-1]<=a[l]&&a[l]<end)){
			cnt++;continue;
		}
		for(int j=pre_l[i];j<num[i]-1;++j){
			if(s[i][j]<=a[l]&&a[l]<e[i][j])
				break;
			if(j+1<num[i]&&e[i][j]<=a[l]&&a[l]<s[i][j+1]){
				pre_l[i]=j;
				cnt++;break;
			}						
		}
	}
	if(cnt>=2&&(m-cnt)<=1)
		return true;
	else
		return false;	
}

bool Check_End(int r){
	int cnt=0;
	for(int i=0;i<m;++i){
		if(num[i]==0){
			cnt++;continue;
		}
		if((start<a[r]&&a[r]<=s[i][0])||(e[i][num[i]-1]<a[r]&&a[r]<=end)){
			cnt++;continue;
		}
		for(int j=pre_r[i];j<num[i]-1;++j){
			if(s[i][j]<a[r]&&a[r]<=e[i][j])
				break;
			if(j+1<num[i]&&e[i][j]<a[r]&&a[r]<=s[i][j+1]){
				pre_r[i]=j;
				cnt++;break;
			}				
		}
	}
	if(cnt>=2&&(m-cnt)<=1)
		return true;
	else
		return false;
}

bool Check_Period(int l,int r){
	ll h1,h2;
	h1=(ll)3600*8640*a[l].year+(ll)3600*720*a[l].month+(ll)3600*24*a[l].day+(ll)3600*a[l].hour+(ll)60*a[l].minute+(ll)a[l].seco;
	h2=(ll)3600*8640*a[r].year+(ll)3600*720*a[r].month+(ll)3600*24*a[r].day+(ll)3600*a[r].hour+(ll)60*a[r].minute+(ll)a[r].seco;	
	if(h2-h1>=3600)
		return true;
	else
		return false;
}

void Output(int l,int r){
	printf("appointment possible from ");
	printf("%02d/%02d/%04d %02d:%02d:%02d to ",
			a[l].month,a[l].day,a[l].year,a[l].hour,a[l].minute,a[l].seco);
	printf("%02d/%02d/%04d %02d:%02d:%02d\n",
			a[r].month,a[r].day,a[r].year,a[r].hour,a[r].minute,a[r].seco);
}

int main()
{
//	freopen("input.txt","r",stdin);
	start=timetable(1800,1,1,0,0,0);
	end=timetable(2200,1,1,0,0,0);
	scanf("%d",&T);
	for(int ccase=1;ccase<=T;++ccase){
		init();
		scanf("%d",&m);
		for(int i=0;i<m;++i){
			scanf("%d",&n);
			num[i]=n;
			for(int j=0;j<n;++j){
				scanf("%d%d%d%d%d%d",&s[i][j].year,&s[i][j].month,&s[i][j].day,&s[i][j].hour,&s[i][j].minute,&s[i][j].seco);
				scanf("%d%d%d%d%d%d",&e[i][j].year,&e[i][j].month,&e[i][j].day,&e[i][j].hour,&e[i][j].minute,&e[i][j].seco);
				getline(cin,tmp);
				a[tot++]=s[i][j];
				a[tot++]=e[i][j];
			}
		}
		printf("Scenario #%d:\n",ccase);
		a[tot++]=start;
		a[tot++]=end;
		sort(a,a+tot);
		int l=0,r=1;
		bool flag=false;
		while(l<tot&&r<tot){
			while(r<tot&&Check_End(r))
				++r;
			--r;
			if(Check_Period(l,r)){
				flag=true;
				Output(l,r);
			}
			l=r+1;
			while(l<tot&&!Check_Start(l))
				++l;
			r=l+1;
		}
		if(!flag)
			printf("no appointment possible\n");
		printf("\n");
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值