K
来源:互联网 发布:手机拍照滤镜软件 编辑:程序博客网 时间:2024/06/10 01:48
Mr. Jones is an exemplary husband. Every Saturday morning Mrs. Jones gives him a list of goods to be bought from the supermarket and he buys exactly what he has been asked for, always choosing the brands with lowest prices. But Mr. Jones hates going to the supermarket on a Saturday, since its aisles are packed with shoppers. He wants to change the way he does his shopping. Instead of going to and fro to buy the products on his wife's list, he will try to get the goods on the list going through each aisle only once, picking up the products in the exact order given in the list. So he asked you to write a
program to help him with his new style of shopping.
Given the information about products available in the supermarket together with their prices in the order in which they appear in Mr. Jones' way and the list of products given by his wife, your program must determine the least cost that he would pay.
Mr. Jones buys the products in the order in which they appear in Mrs. Jones' list and he never goes back as he walks down the aisles. Therefore, if he buys the i-th product on his way as the j-th item on the list, the next product to be bought is the (j+1)-th item of the list ? and it must be bought from the products that come after i in his path. The figure below shows an example where products are identi-fied by integers. Note that different brands of the same product may appear separately. In the example Mr. Jones must buy products 1,1,2,20 (notice that product 1 appears twice in the list). For the example,the least cost for Mr. Jones following his constraints is 21.30. Notice that with this new way of shopping it may be impossible for Mr. Jones to buy all the goods on Mrs. Jones list; in that case, your program should warn Mr. Jones.
program to help him with his new style of shopping.
Given the information about products available in the supermarket together with their prices in the order in which they appear in Mr. Jones' way and the list of products given by his wife, your program must determine the least cost that he would pay.
Mr. Jones buys the products in the order in which they appear in Mrs. Jones' list and he never goes back as he walks down the aisles. Therefore, if he buys the i-th product on his way as the j-th item on the list, the next product to be bought is the (j+1)-th item of the list ? and it must be bought from the products that come after i in his path. The figure below shows an example where products are identi-fied by integers. Note that different brands of the same product may appear separately. In the example Mr. Jones must buy products 1,1,2,20 (notice that product 1 appears twice in the list). For the example,the least cost for Mr. Jones following his constraints is 21.30. Notice that with this new way of shopping it may be impossible for Mr. Jones to buy all the goods on Mrs. Jones list; in that case, your program should warn Mr. Jones.
Your program should process data for several shopping sessions. The first line in the description of a shopping session contains two integers M and N; M indicates the number of items in Mrs. Jones' list (1 <= M <= 100) and N represents the total number of products available in the supermarket (1 <= N <= 100,000). The next line contains M integers Xi representing the list of products in Mrs. Jones' list (1 <= Xi <= 100,000, 1 <= i <= M). Then N lines follow, representing the supermarket products in the order in which they appear in Mr. Jones' way. Each of those lines contains an integer K and a real P which represent respectively a product identifier and its price (1 <= K <= 100,000). The end of input is indicated by M = N = 0.
For each shopping session in the input, your program should produce one line of output, containing the least cost that Mr. Jones would pay. If it is not possible to buy all the goods for the session, print the word 'Impossible'. The cost must be printed as a real number with two-digit precision, and the last decimal digit must be rounded. The input will not contain test cases where differences in rounding are significant.
4 81 1 2 202 0.291 0.3020 0.151 1.005 0.052 10.0020 20.0020 10.002 51 23 1.004 1.002 0.011 1.002 1.502 31 22 0.051 10.001 3.000 0
21.302.50Impossible
代码如下:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;int main(){ int n,m,i,j; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0 && m==0) break; double dp[105]; int b[105]; for(int i=1; i<=n; i++) { scanf("%d",&b[i]); dp[i] = 999999; } int k; double q; dp[0]=0; for( i=1; i<=m; i++) { scanf("%d%lf",&k,&q); for( j=1;j<=n;j++) { //品牌是不是一致的, 前一个品牌价格加上现在的品牌比较 , 判断前面一个是不是出现过,在买你这个品牌 //判断出现过没, //从后面判断,可以避免相同的品牌,如1 0.3 1 1.00的情况,前面已经最小了,所以要从更后面的新值 //由于数据为可重复的,并且价格为实数,所以不好标记,就从后面开始更新。 if(k==b[j] && dp[j-1]!=999999 && dp[j-1]+q<dp[j]) { dp[j] = dp[j-1]+q; } } } //判断最后一个物品是不是买了, if(dp[n]==999999) { printf("Impossible\n"); } else { printf("%.2f\n",dp[n]); } } return 0;}
阅读全文
0 0
- k
- k
- k,
- k
- k
- k
- K -
- K
- K
- K
- K
- K
- k
- K
- K
- K
- K
- K
- python 各种问题处理更新 何不一次性搞定全部编码知识
- “史诗级”的Volley源码您真的彻底弄懂了?
- android 为TextView的部分文字设置超链接样式并监听点击事件
- 多项列表无限滚动
- 第11讲 mysql查询数据 并展示出来
- K
- WireShark: “No interfaces found” on Microsoft Windows
- 百练_2688:求字母的个数
- 实习笔记1
- HDU2010 水仙花数
- android 使用zxing生成中间嵌套logo的二维码
- JqGrid选中行、取消选中行、获得选中行数据
- 第12讲 通过php程序添加数据
- 关于箭头函数的 this 绑定