POJ 刷题系列:2586. Y2K Accounting Bug
来源:互联网 发布:魔兽世界3.3.5数据库 编辑:程序博客网 时间:2024/06/08 07:42
POJ 刷题系列:2586. Y2K Accounting Bug
传送门:2586. Y2K Accounting Bug
题意:
有一个公司由于某个病毒使公司赢亏数据丢失,但该公司每月的 赢亏是一个定数,要么一个月赢利s,要么一月亏d。现在ACM只知道该公司每五个月有一个赢亏报表,而且每次报表赢利情况都为亏。在一年中这样的报表总共有8次(1到5,2到6,…,8到12),现在要编一个程序确定当赢s和亏d给出,并满足每张报表为亏的情况下,全年公司最高可赢利多少,若存在,则输出多多额,若不存在,输出”Deficit”。
思路:
暴力搜索,因为12个月,每个月只存在两种状态,要么赢利要么亏,所以有2^12种状态,在这些状态中再过滤出8次报表都为负的状态,然后更新一波最大值即可。
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.Arrays;import java.util.Map;import java.util.StringTokenizer;public class Main{ String INPUT = "./data/judge/201712/P2586.txt"; public static void main(String[] args) throws IOException { new Main().run(); } int[] stateToWindow(int state) { int[] window = new int[12]; for (int i = 0; i < 12; ++i) { window[i] = (state >> i) & 1; } return window; } boolean valid(double s, double d, int[] window) { double sum = 0; for (int i = 0; i < 5; ++i) { if (window[i] == 1) { sum += s; } else { sum += d; } } if (sum >= 0) return false; for (int i = 1; i + 4 < 12; ++i) { if (window[i - 1] == 1) sum -= s; else sum -= d; if (window[i + 4] == 1) sum += s; else sum += d; if (sum >= 0) return false; } return true; } double total(double s, double d, int[] window) { double sum = 0; for (int i = 0; i < 12; ++i) { if (window[i] == 1) sum += s; else sum += d; } return sum; } void read() { while(more()) { double s = nd(); double d = nd(); d = -d; double max = 0; for (int i = 0; i < 1 << 12; ++i) { int[] window = stateToWindow(i); if (valid(s, d, window)) { max = Math.max(max, total(s, d, window)); } } if (max > 0) out.printf("%.0f\n", max); else { out.println("Deficit"); } } } FastScanner in; PrintWriter out; void run() throws IOException { boolean oj; try { oj = ! System.getProperty("user.dir").equals("F:\\oxygen_workspace\\Algorithm"); } catch (Exception e) { oj = System.getProperty("ONLINE_JUDGE") != null; } InputStream is = oj ? System.in : new FileInputStream(new File(INPUT)); in = new FastScanner(is); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); read(); out.flush(); if (!oj){ System.out.println("[" + (System.currentTimeMillis() - s) + "ms]"); } } public boolean more(){ return in.hasNext(); } public int ni(){ return in.nextInt(); } public long nl(){ return in.nextLong(); } public double nd(){ return in.nextDouble(); } public String ns(){ return in.nextString(); } public char nc(){ return in.nextChar(); } class FastScanner { BufferedReader br; StringTokenizer st; boolean hasNext; public FastScanner(InputStream is) throws IOException { br = new BufferedReader(new InputStreamReader(is)); hasNext = true; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { hasNext = false; return "##"; } } return st.nextToken(); } String next = null; public boolean hasNext(){ next = nextToken(); return hasNext; } public int nextInt() { if (next == null){ hasNext(); } String more = next; next = null; return Integer.parseInt(more); } public long nextLong() { if (next == null){ hasNext(); } String more = next; next = null; return Long.parseLong(more); } public double nextDouble() { if (next == null){ hasNext(); } String more = next; next = null; return Double.parseDouble(more); } public String nextString(){ if (next == null){ hasNext(); } String more = next; next = null; return more; } public char nextChar(){ if (next == null){ hasNext(); } String more = next; next = null; return more.charAt(0); } } static class D{ public static void pp(int[][] board, int row, int col) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { sb.append(board[i][j] + (j + 1 == col ? "\n" : " ")); } } System.out.println(sb.toString()); } public static void pp(char[][] board, int row, int col) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { sb.append(board[i][j] + (j + 1 == col ? "\n" : " ")); } } System.out.println(sb.toString()); } } static class ArrayUtils { public static void fill(int[][] f, int value) { for (int i = 0; i < f.length; ++i) { Arrays.fill(f[i], value); } } public static void fill(int[][][] f, int value) { for (int i = 0; i < f.length; ++i) { fill(f[i], value); } } public static void fill(int[][][][] f, int value) { for (int i = 0; i < f.length; ++i) { fill(f[i], value); } } } static class Num{ public static <K> void inc(Map<K, Integer> mem, K k) { if (!mem.containsKey(k)) mem.put(k, 0); mem.put(k, mem.get(k) + 1); } }}
阅读全文
0 0
- POJ 刷题系列:2586. Y2K Accounting Bug
- POJ 2586.Y2K Accounting Bug
- POJ Y2K Accounting Bug
- 【POJ】Y2K Accounting Bug
- Y2K Accounting Bug POJ
- Y2K Accounting Bug poj
- poj 2586 Y2K Accounting Bug【贪心】【刷题计划】
- POJ 2586 Y2K Accounting Bug
- poj-2586-Y2K Accounting Bug
- poj 2586 Y2K Accounting Bug
- POJ 2586 Y2K Accounting Bug
- POJ 2586 Y2K Accounting Bug
- poj - 2586 - Y2K Accounting Bug
- 水题 poj Y2K Accounting Bug
- POJ 2586 Y2K Accounting Bug
- poj 2586 Y2K Accounting Bug
- poj 2586 Y2K Accounting Bug
- poj 2586 Y2K Accounting Bug
- JSP基本知识点
- C#单例的多种写法
- (python3)1024. 科学计数法 (20分)
- 【微服务架构】SpringCloud之Eureka(服务注册和服务发现基础篇)(二)
- 朱金付第十一章编程
- POJ 刷题系列:2586. Y2K Accounting Bug
- SVG之旅:SVG的图层和渲染顺序
- bat学习笔记-2017年11月18日
- GitHub 与 Git 的区别
- phpStrom上传文件到部署的远程服务器
- 给你一个 n 个点,m 条边的无向图,求至少要在这个的基础上加多少条无向边使得任意两个点可达~
- 隐晦规则
- ios-手动实现KVO简单版
- 【微服务架构】SpringCloud之Eureka(注册中心集群篇)(三)