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,理论上两个线程运 ...
随机推荐
- C语言递归之二叉树的最小深度
题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例 输入:[3,9,20,null,null,15,7] ...
- centos7 下 安装GeoIP2,在nginx中根据ip地址对应的国家转发请求
最近有个需求是根据用户的地理位置,访问不同的服务器,比如国外用户访问国外的服务器,国内的用户访问国内的服务器,实现的思路主要两种: 智能dns,这个需要在阿里云中注册为企业版才有提供 nginx中使用 ...
- urllib模块提供的urlretrieve()函数使用
urllib模块提供的urlretrieve()函数,urlretrieve()方法直接将远程的数据下载到本地 注意:若是网站有反爬虫的话这个函数会返回 403 Forbidden 参数url:传入的 ...
- 一个简易git服务器的搭建
查看本机ssh公钥,生成公钥 查看ssh公钥方法: 1. 打开git bash窗口 2. 进入.ssh目录: cd ~/.ssh 3. 找到id_rsa.pub文件: ls 4. 查看公钥:cat i ...
- C语言实现顺序栈
C语言实现顺序栈,顺便加深刻++i,++i的区别 #include <stdio.h>#include <stdlib.h>#define maxsize 100/*写在前面的 ...
- Git和TortoiseGit
1.简介 Git是一个开源的分布式版本控制系统,用于敏捷高效的处理任何或大或小的项目.它采用了分布式版本库的方式,不必服务器端软件支持. 2.Git和Svn的区别 1.Git 是分布式的,SVN 不是 ...
- 【Unity|C#】基础篇(21)——常用类
- python xlrd 模块(获取Excel表中数据)
python xlrd 模块(获取Excel表中数据) 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了pyt ...
- PIE-SDK For C++栅格数据集的读写
1.功能简介 栅格数据包含很多信息,在数据的运用中需要对数据的信息进行读取或写入,目前PIE SDK支持多种数据格式的数据读取和写入,下面对栅格数据格式的数据读写功能进行介绍. 2.功能实现说明 2. ...
- [SDOI2012] Longge的问题 - 欧拉函数
求 \(\sum\limits_{i=1}^{n}gcd(i,n)\) Solution 化简为 \(\sum\limits_{i|n}^{n}φ(\dfrac{n}{i})i\) 筛出欧拉函数暴力求 ...