Java-POJ1010-STAMP
说良心话,题目不难,但是题目真的很不好懂,解读一下吧
题意:
读入分两行,第一行为邮票面额(面额相同也视为种类不同)以0结束,第二行为顾客要求的面额,以0结束
要求:每个顾客最多拿4张邮票,并求最优解
输出:对于每个顾客要求输出一行
对于最优解的定义:
1.要求邮票种类尽量多(原则上每种邮票可以无限供应)
2.满足条件1的情况下,要求顾客拿到的邮票张数尽量少
3.满足条件2的情况下,要求顾客拿到的邮票中,面额最大的邮票面额越大越好
4.若满足以上3个条件的解不唯一,输出该顾客拿到的邮票种类数,以及“tie”
5.无法满足顾客要求输出“ ---- none”
PS:(聪明人才能看到我给的提示)
bug1,由于poj没有specail judge,面额的拼凑方案必须按从小到大排序
bug2,邮票种类远不止题目中给的25种,数组开小了会RE,建议开大点,第一次开了60都不够,坑爹啊!
package poj.ProblemSet; import java.util.Scanner; public class poj1010 { public static final int MAXN = 100;
public static int[] stamp_time = new int[MAXN];
public static int[] stamp = new int[MAXN];
public static int[] customer = new int[MAXN];
public static int[] ans = new int[MAXN];
public static int stamp_n, customer_n, flag;
public static int ans_type, ans_total_time, ans_max_stamp;
public static int now_type, now_total_time, now_max_stamp; public static void query() {
now_type = now_total_time = now_max_stamp = 0;
for (int i = 1; i <= stamp_n; i++) {
if (stamp_time[i] > 0) {
now_type++;
now_total_time += stamp_time[i];
if (stamp[i] > now_max_stamp)
now_max_stamp = stamp[i];
}
}
} public static void update(int type) {
ans_type = type;
ans_total_time = ans_max_stamp = 0;
for (int i = 1; i <= stamp_n; i++) {
ans[i] = stamp_time[i];
if (ans[i] > 0) {
ans_total_time += ans[i];
if (stamp[i] > ans_max_stamp)
ans_max_stamp = stamp[i];
}
}
} public static void dfs(int cost, int type, int a, int num) {
if (num > 4 || cost < 0) return;
if (cost == 0) {
if (type > ans_type) { flag = 0;update(type); }
else if (type == ans_type) {
query();
if (now_total_time < ans_total_time) { flag = 0;update(type); }
else if (now_total_time == ans_total_time) {
if (now_max_stamp > ans_max_stamp) { flag = 0;update(type); }
else if (now_max_stamp == ans_max_stamp) flag = 1;
}
} }
for (int i = a; i <= stamp_n; i++) {
if (i == a) {
if (type == 0) { stamp_time[a]++;dfs(cost - stamp[a], 1, a, num + 1);stamp_time[a]--; }
else { stamp_time[a]++;dfs(cost - stamp[a], type, a, num + 1);stamp_time[a]--; }
}
else { stamp_time[i]++;dfs(cost - stamp[i], type + 1, i, num + 1);stamp_time[i]--; }
}
} public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
stamp_n = customer_n = 0;
for (int i = 0; i < MAXN; i++) stamp_time[i] = stamp[i] = customer[i] = 0;
do stamp[++stamp_n] = cin.nextInt(); while (stamp[stamp_n] != 0);
do customer[++customer_n] = cin.nextInt(); while (customer[customer_n] != 0);
stamp_n--;
customer_n--;
for (int i = 1; i <= stamp_n; i++)
for (int j = i + 1; j <= stamp_n; j++)
if (stamp[i] > stamp[j]) {
int temp = stamp[i];
stamp[i] = stamp[j];
stamp[j] = temp;
}
for (int i = 1; i <= customer_n; i++) {
ans_type = ans_total_time = ans_max_stamp = flag = 0;
for (int j = 0; j < MAXN; j++) ans[j] = 0;
dfs(customer[i], 0, 1, 0);
if (flag == 1) System.out.println(customer[i] + " (" + ans_type + "): tie");
else if (ans_type > 0) {
System.out.print(customer[i] + " (" + ans_type + "):");
for (int j = 1; j <= stamp_n; j++) if (ans[j] > 0) while (ans[j]-- > 0) System.out.print(" " + stamp[j]);
System.out.println();
}
else System.out.println(customer[i] + " ---- none");
}
}
}
}
Java-POJ1010-STAMP的更多相关文章
- Protocol Buffers in HBase
For early Hbase developers, it is often a nightmare to understand how the different modules speak am ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- java 调用阿里云短信接口,报InvalidTimeStamp.Expired : Specified time stamp or date value is expired.
官网解释: 问题所在: 自己的电脑(或者服务器) 的时间与阿里云的服务器时间 相差15分钟了. 解决方法 : 把自己的电脑时间 (或者服务器)的时间 改成标准的北京时间就行了.
- Java的多线程机制系列:(二)缓存一致性和CAS
一.总线锁定和缓存一致性 这是两个操作系统层面的概念.随着多核时代的到来,并发操作已经成了很正常的现象,操作系统必须要有一些机制和原语,以保证某些基本操作的原子性.首先处理器需要保证读一个字节或写一个 ...
- java 日期转时间戳,时间戳转为日期
package date; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Dat ...
- java时间戳
1.时间戳的定义 时间戳是指文件属性里的创建.修改.访问时间. 数字时间戳技术是数字签名技术一种变种的应用.在电子商务交易文件中,时间是十分重要的信息.在书面合同中,文件签署的日期和签名一样均是十分重 ...
- Java构建工具Ant小记(一)
Ant简介 Ant是基于java的构建工具.理论上来说它类似与make工具,但是却克服了make的一些固有的缺陷. 传统的Make是基于操作系统shell的构建工具,虽然也可以基于工作的os对make ...
- java之Date
1.java时间处理 package com.bmkit.util.date; import java.text.DateFormat; import java.text.ParseException ...
- Java CAS 和ABA问题
独占锁:是一种悲观锁,synchronized就是一种独占锁,会导致其它所有需要锁的线程挂起,等待持有锁的线程释放锁. 乐观锁:每次不加锁,假设没有冲突去完成某项操作,如果因为冲突失败就重试,直到成功 ...
- Java中Atomic包的实现原理及应用
1. 同步问题的提出 假设我们使用一个双核处理器执行A和B两个线程,核1执行A线程,而核2执行B线程,这两个线程现在都要对名为obj的对象的成员变量i进行加1操作,假设i的初始值为0,理论上两个线程运 ...
随机推荐
- 免费生成二维码接口,可直接嵌入到web项目中,附带嵌入方法,任意颜色二维码,任意大小二维码!
在线体验连接:http://www.zhaimaojun.top/qrcode/ 你是否在项目中寻找方便而且免费的可以直接嵌入到项目中的二维码生成工具呢?你找到了这里,说明你已经找到了!不要犹豫直接拿 ...
- 纪中5日T3 1566. 幸运锁(lucky.pas/c/cpp)
1566. 幸运锁(lucky.pas/c/cpp) 题目描述 有一把幸运锁,打开它将会给你带来好运,但开锁时需要输入一个正整数(没有前导0).幸运锁有一种运算,对于一个正整数,返回他的相邻两位数字间 ...
- 纪中5日T2 1565. 神秘山庄
1565. 神秘山庄 (Standard IO) 原题 题目描述 翠亨村是一个神秘的山庄,并不是因为它孕育了伟人孙中山,更神秘的是山庄里有N只鬼.M只兔子,当然还有你.其中每秒钟: 1. 恰有两个生物 ...
- LOJ#6038. 「雅礼集训 2017 Day5」远行 [LCT维护子树的直径]
树的直径一定是原联通块4个里的组合 1.LCT,维护树的直径,这题就做完了 2.直接倍增,lca啥的求求距离,也可以吧- // powered by c++11 // by Isaunoya #inc ...
- 1.(group by)如何让group by分组后,每组中的所有数据都显示出来
问题描述:表如下,如何让这个表按device_id这个字段分组,且组中的每条数据都查寻出来?(假如说这个表名为:devicedata) 错误答案:select * from devicedata GR ...
- CCS 5.5下载地址http://www.dianyuan.com/bbs/1492792.html
http://www.dianyuan.com/bbs/1492792.html https://pan.baidu.com/s/1eQtIRK2?errno=0&errmsg=Auth%20 ...
- vue-routerV3.1版本报错:message: "Navigating to current location ("/home") is not allowed",
出现这个错误的原因是,在路由跳转的时候两次push的path地址相同 解决方法两种: 1.切换版本回3.0版本 2.在你引了vue-router的js文件里加上如下代码即可 import VueRou ...
- 线段树(lazy标记)-- 模板
], lazy[MAXN << ]; void PushUp(int rt) { ans[rt] = ans[rt << ] + ans[rt << | ]; } ...
- vsftpd最详细的配置文件
vsftpd作为一个主打安全的FTP服务器,有很多的选项设置.下面介绍了vsftpd的配置文件列表,而所有的配置都是基于vsftpd.conf这个配置文件的.本文将提供完整的vsftpd.conf的中 ...
- 点击按钮出现60秒倒计时,JS(按钮)
<script type="text/javascript"> var countdown=60; function settime(val) { var e = $( ...