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,理论上两个线程运 ...
随机推荐
- 关于Spring注入参数到static静态参数失败问题处理。解决Autowired annotation is not supported on static fields的问题
直接贴代码 把注入参数的注解加到set方法上面去即可. 因为这是一个工具类用到的config,所以一开始没有加@Component,还是依然为空,加上之后就正常能注入了
- P3759 [TJOI2017]不勤劳的图书管理员 [树套树]
树套树是什么啊我不知道/dk 我只知道卡常数w // by Isaunoya #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC o ...
- 敌兵布阵 HDU - 1166 板子题
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...
- c语言中 char* 和 unsigned char* 的区别浅析(转)
原文:https://blog.csdn.net/guotianqing/article/details/77341657 背景最近在项目中遇到了一个编译警告,是因为定义的变量为char[],而在使用 ...
- SpringBoot的应运而生
随着动态语言的流行(Ruby,Groovy,Scala,Node.js),java的开发显得格外的笨重,繁多的配置,低下的效率,复杂的部署流程以及第三方技术集成难度大.springboot应运而生,使 ...
- hadoop基本组件原理小总结
Hadoop基础知识小总结 这是本人(学生党)在学习hadoop半个学期后根据教科书后习题做的一个小总结,如有发现错误还请各位海涵并指出,我会及时改过来的,谢谢! 目录 Hadoop基础知识小总结. ...
- 第一个,net core项目,一起入门 !!!
最近项目上开始使用.net core,新的项目,熟悉的东西比较多,现在花点时间来梳理一下,重头开始搭建一个.net core项目.哈哈,这个相对老手来说,估计会觉得小儿科,没事,也就当一次分享总结罢了 ...
- harbor仓库部署时启用https时的常见错误KeyError: 'certificate'等
出现 KeyError: 'certificate' 错误 先确认你的配置是否正确,例如harbor.yml里的https证书位置是否正确,证书是否正常无误 如果上述无误确反复报错,请确认你的harb ...
- 针对wordpress CMS的信息收集
如果发现一个站点用的是wordpress管理系统的话, 可以试试默认的后台地址:/wp-admin/ 访问后自动跳转置 后台登录界面 用户名收集 :/?author=1 依次访问/author=1 , ...
- LeetCode136. 只出现一次的数字(异或)
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...