博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4451 Dressing
阅读量:6261 次
发布时间:2019-06-22

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

Dressing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1112    Accepted Submission(s): 471

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
 
 
 

就是有N件衣服,M件裤子,K件鞋子。

有一些衣服和裤子是搭配不和谐的,有一些裤子和鞋子是搭配不和谐的。

问有多少种搭配衣服+裤子+鞋子,使得不存在不和谐的。

 

很简单,不要想得太多。

 

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

可以降低一维。

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

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

还可以再降低一维。

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

复杂度O(M).

代码,按照第二种思路写的。

 

#include
#include
#include
using namespace std;char str1[20],str2[20];int d1,d2;int m,n,k,p;int a[1005][1005],b[1005][1005],c[1005];int main(){ 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

 

转载地址:http://zazpa.baihongyu.com/

你可能感兴趣的文章
网卡驱动
查看>>
kinect sdk开发入门WPFdemo笔记
查看>>
Server.Transfer详细解释
查看>>
java单链表的增、删、查操作
查看>>
The working copy at 'xxx' is too old 错误解决
查看>>
jadclipse
查看>>
// 1.什么是JOSN?
查看>>
SQL语句详细汇总
查看>>
如何保护.net中的dll文件(防破解、反编译)
查看>>
Python 装饰器
查看>>
Docker 网络模式
查看>>
[POI2013]Usuwanka
查看>>
problem-solving-with-algorithms-and-data-structure-usingpython(使用python解决算法和数据结构) -- 算法分析...
查看>>
nodejs + CompoundJS 资源
查看>>
转:C#并口热敏小票打印机打印位图
查看>>
scau 17967 大师姐唱K的固有结界
查看>>
spring之<bean>实例化
查看>>
hereim_美句_2
查看>>
蓝桥杯2017国赛JAVAB组 填字母游戏 题解
查看>>
25.安装配置phantomjs
查看>>