博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4451 简单搭配问题
阅读量:6435 次
发布时间:2019-06-23

本文共 2420 字,大约阅读时间需要 8 分钟。

Dressing

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 14   Accepted Submission(s) : 7
Problem Description
Wangpeng has N clothes, M pants and K shoes so theoretically he can have N×M×K different combinations of dressing.
One day he wears his pants Nike, shoes Adiwang to go to school happily. When he opens the door, his mom asks him to come back and switch the dressing. Mom thinks that pants-shoes pair is disharmonious because Adiwang is much better than Nike. After being asked to switch again and again Wangpeng figure out all the pairs mom thinks disharmonious. They can be only clothes-pants pairs or pants-shoes pairs.
Please calculate the number of different combinations of dressing under mom’s restriction.
 
Input
There are multiple test cases. For each case, the first line contains 3 integers N,M,K(1≤N,M,K≤1000) indicating the number of clothes, pants and shoes. Second line contains only one integer P(0≤P≤2000000) indicating the number of pairs which mom thinks disharmonious. Next P lines each line will be one of the two forms“clothes x pants y” or “pants y shoes z”. The first form indicates pair of x-th clothes and y-th pants is disharmonious(1≤x≤N,1 ≤y≤M), and second form indicates pair of y-th pants and z-th shoes is disharmonious(1≤y≤M,1≤z≤K). Input ends with “0 0 0”. It is guaranteed that all the pairs are different.
 
Output
For each case, output the answer in one line.
 
Sample Input
 
2 2 2 0 2 2 2 1 clothes 1 pants 1 2 2 2 2 clothes 1 pants 1 pants 1 shoes 1 0 0 0
 
Sample Output
 
8 6 5
 
Source
2012 Asia JinHua Regional Contest

有N件衣服,M件裤子,K件鞋子。有一些衣服和裤子是搭配不和谐的,有一些裤子和鞋子是搭配不和谐的。问有多少种搭配衣服+裤子+鞋子,使得不存在不和谐的。

最直接的方法是枚举每一件组合,找出符合的,复杂度O(N*M*K),这个肯定是超时的。。。

可以降低一维。

枚举每一件衣服和裤子的组合,如果衣服和裤子可以搭配,则看该条裤子可以和多少件鞋子搭配。累加就可以了。

这个的复杂度是O(N*M).这个对于这题已经可以AC了,虽然效率没有很高。

还可以再降低一维。

枚举每条裤子,可以和这条裤子搭配的衣服数乘以可以和这条衣服搭配的鞋子数。累加起来就是答案。

复杂度O(M).

第二种就可以AC,所以按照第二种来写:

#include
#include
#include
using namespace std;const int maxn = 1000+5;char str1[20],str2[20];int d1,d2;int m,n,k,p;int a[maxn][maxn],b[maxn][maxn],c[maxn];int main(){ //freopen("in.txt","r",stdin); int i,j; while(scanf("%d%d%d",&m,&n,&k) && (m || n || k)){ scanf("%d",&p); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); for(i=0;i

转载于:https://www.cnblogs.com/zhangmingzhao/p/7256736.html

你可能感兴趣的文章
$.ajax同步请求会阻塞js进程
查看>>
Postman 网络调试工具
查看>>
Python教程6
查看>>
zabbix实现自动发现功能添加磁盘监控
查看>>
mysql8.0.14 安装
查看>>
1039. 到底买不买(20)
查看>>
android笔试题一
查看>>
【JavaEE企业应用实战学习记录】getConnListener
查看>>
了解轮询、长轮询、长连接、websocket
查看>>
bzoj2427[HAOI2010]软件安装
查看>>
WPF个人助手更新
查看>>
NLPIR技术助力中文智能数据挖掘
查看>>
python操作redis--------------数据库增删改查
查看>>
Android中仿IOS提示框的实现
查看>>
php初学第一课
查看>>
Windows下与Linux下编写socket程序的区别 《转载》
查看>>
java学习笔记 --- IO(3)
查看>>
Mysql 的FIND_IN_SET函数慢的忧化
查看>>
Web service是什么?
查看>>
python 问题集合
查看>>