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);        }    }}

这里写图片描述

原创粉丝点击