说良心话,题目不难,但是题目真的很不好懂,解读一下吧

题意:

  读入分两行,第一行为邮票面额(面额相同也视为种类不同)以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的更多相关文章

  1. Protocol Buffers in HBase

    For early Hbase developers, it is often a nightmare to understand how the different modules speak am ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. java 调用阿里云短信接口,报InvalidTimeStamp.Expired : Specified time stamp or date value is expired.

    官网解释: 问题所在: 自己的电脑(或者服务器) 的时间与阿里云的服务器时间 相差15分钟了. 解决方法 : 把自己的电脑时间 (或者服务器)的时间 改成标准的北京时间就行了.

  4. Java的多线程机制系列:(二)缓存一致性和CAS

    一.总线锁定和缓存一致性 这是两个操作系统层面的概念.随着多核时代的到来,并发操作已经成了很正常的现象,操作系统必须要有一些机制和原语,以保证某些基本操作的原子性.首先处理器需要保证读一个字节或写一个 ...

  5. java 日期转时间戳,时间戳转为日期

    package date; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Dat ...

  6. java时间戳

    1.时间戳的定义 时间戳是指文件属性里的创建.修改.访问时间. 数字时间戳技术是数字签名技术一种变种的应用.在电子商务交易文件中,时间是十分重要的信息.在书面合同中,文件签署的日期和签名一样均是十分重 ...

  7. Java构建工具Ant小记(一)

    Ant简介 Ant是基于java的构建工具.理论上来说它类似与make工具,但是却克服了make的一些固有的缺陷. 传统的Make是基于操作系统shell的构建工具,虽然也可以基于工作的os对make ...

  8. java之Date

    1.java时间处理 package com.bmkit.util.date; import java.text.DateFormat; import java.text.ParseException ...

  9. Java CAS 和ABA问题

    独占锁:是一种悲观锁,synchronized就是一种独占锁,会导致其它所有需要锁的线程挂起,等待持有锁的线程释放锁. 乐观锁:每次不加锁,假设没有冲突去完成某项操作,如果因为冲突失败就重试,直到成功 ...

  10. Java中Atomic包的实现原理及应用

    1. 同步问题的提出 假设我们使用一个双核处理器执行A和B两个线程,核1执行A线程,而核2执行B线程,这两个线程现在都要对名为obj的对象的成员变量i进行加1操作,假设i的初始值为0,理论上两个线程运 ...

随机推荐

  1. 关于Web服务器时间修改后遗症

    在开发过程中,遇到一个问题,在本地测试成功,发布服务器后,总是 莫名的登录失效. 原因,服务器端代码设置了cookie的过期时间为1天后,而服务器的时间与浏览器端机器时间不一致.导致浏览器判断 coo ...

  2. Easyui-Treegrid使用注意事项-sunziren

    版权声明:本文为sunziren原创文章,博客园首发,转载务必注明出处以及作者名称. 最近,工作中有一个网页需要用到前端框架easyui的treegrid组件,因此我对这个treegird研究了一段时 ...

  3. github的版本控制master和branch develop

    一.git版本控制原理 master(主分支), develop(分支),虽然是主分支和分支,却是平级关系,develop可以理解为开发库,master为生产库. 本地版本:master, devel ...

  4. 权限问题 <customErrors> 标记的“mode”属性设置为“Off”

    引用 权限问题 <customErrors> 标记的“mode”属性设置为“Off”. 权限问题标记的“mode”属性设置为“Off”.说明: 服务器上出现应用程序错误.此应用程序的当前自 ...

  5. 全网VIP视频解析接口

    全网VIP视频在线解析可以免费观看[腾讯vip视频.爱奇艺vip视频.优酷VIP视频.土豆VIP视频.乐视VIP视频.芒果VIP视频]等等...可以vip免费观看.去广告等等. https vip视频 ...

  6. 广搜 BFS()

    极其简陋的BFS模板 void BFS(???){ queue<node>q; node start, next; start = ???; q.push(start); while(!q ...

  7. php 文件追加写入

    //追加写入 file_put_contents('xml.text',json_encode($postObj,JSON_FORCE_OBJECT).PHP_EOL,FILE_APPEND);

  8. 如何通过注解方式给项目添加Swagger功能

    在Java后端,每次开发一个新的接口后需要自测,此时可以借助Swagger功能很好地完成自测,下面将通过注解的方式来添加Swagger. (1)代码部分 package com.bien.edge; ...

  9. Git操作时遇到的一些问题和相应的处理方式

    Q1:如何解决冲突/避免冲突 A1:执行git fetch之后,本地可能会存在冲突. 如果希望合并本地修改内容,需要执行git merge.不过当有修改内容未提交时,不能merge,要么把修改内容提交 ...

  10. 在阿里云IIS服务器上安装证书

    在IIS服务器上安装证书 您可将下载的阿里云SSL证书安装到IIS服务器上,使您的IIS服务器支持HTTPS安全访问. 一.前提条件 申请证书时需要选择 系统自动创建CSR. 申请证书时如果选择手动创 ...