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

题意:

  读入分两行,第一行为邮票面额(面额相同也视为种类不同)以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. liner-classifiers-SVM

    1支持向量机 参考看了这篇文章你还不懂SVM你就来打我 第一遍看完确实有想打死作者的冲动,但是多看几遍之后,真香~ [SVM---这可能是最直白的推导了] 个人觉得这篇文章讲的很清楚,条理清晰,数学推 ...

  2. python3练习100题——039

    原题链接:http://www.runoob.com/python/python-exercise-example39.html 题目:有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数 ...

  3. Winfom递归绑定树节点

    /// <summary> /// 绑定树节点 /// </summary> /// <param name="pid"></param& ...

  4. LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表

    文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...

  5. 【安卓开发】Webview简单使用

    什么是WebView? 答:Android内置webkit内核的高性能浏览器,而WebView则是在这个基础上进行封装后的一个 控件,WebView直译网页视图,我们可以简单的看作一个可以嵌套到界面上 ...

  6. Jenkins +Ant +Jmeter(apache-jmeter-5.1.1)自动化性能测试平台

    1.安装配置好Jdk, 下载网址:https://www.cr173.com/soft/33894.html 2.Jmeter下载地址:http://jmeter.apache.org/downloa ...

  7. linq 查询-“必须是可缩小的节点”

    出现此错误的情况 查询中先使用分组方法GroupBy()=>Select()=>OrderBy()=>ToList()(实现分组后排序) 解决方法: 排序OrderBy()应该放在S ...

  8. Oracle - 拼接多个字段 - wm_concat()函数

    Oracle wm_concat()函数oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并如:s ...

  9. 占位 DL

    占位 DL include: DL404

  10. 二分-F - Aggressive cows

    F - Aggressive cows Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. ...