usaco 4.3.1 Buy Low, Buy Lower
来源:互联网 发布:软件团队项目 编辑:程序博客网 时间:2024/05/01 14:41
/*ID: daniel.20LANG: JAVATASK: buylow*/ import java.util.*;import java.io.*;import java.math.BigInteger; class problem2{ StringBuilder sb = new StringBuilder(); int n; int stocks[]; int dp[]; BigInteger arr[]; long start = System.currentTimeMillis(); int max = 1; BigInteger result=BigInteger.ZERO; void solver() throws IOException{ BufferedReader reader = new BufferedReader(new FileReader("buylow.in")); StringTokenizer st = new StringTokenizer(reader.readLine()); n = Integer.valueOf(st.nextToken()); stocks = new int[n]; dp = new int[n]; arr = new BigInteger[n]; Arrays.fill(dp, 1); Arrays.fill(arr, BigInteger.ZERO); arr[0] = BigInteger.ONE; String tmp; int len=0; while((tmp=reader.readLine())!=null){ st = new StringTokenizer(tmp); while(st.hasMoreTokens()){ stocks[len++]=Integer.valueOf(st.nextToken()); } } for(int i=1;i<n;i++){ for(int j=0;j<i;j++){ if(stocks[j]>stocks[i]){ if(dp[i]<dp[j]+1){ dp[i]=dp[j]+1; } } } HashMap<Integer,BigInteger> map = new HashMap<Integer,BigInteger>(); for(int j=0;j<i;j++){ if(stocks[j]>stocks[i]){ if(dp[i]==dp[j]+1){ int item = stocks[j]; if(!map.containsKey(item)){ arr[i]=arr[i].add(arr[j]); map.put(item, arr[j]); }else if(arr[j].compareTo(map.get(item))>0){ arr[i]=arr[i].subtract(map.get(item)); arr[i]=arr[i].add(arr[j]); map.put(item, arr[j]); } } } } if(arr[i].compareTo(BigInteger.ZERO)==0) arr[i]=BigInteger.ONE; if(dp[i]>max){ max = dp[i]; } } HashMap<Integer,BigInteger> map1 = new HashMap<Integer,BigInteger>(); for(int i=0;i<n;i++){ if(dp[i]==max){ int temp = stocks[i]; if(!map1.containsKey(temp)){ result=result.add(arr[i]); map1.put(temp, arr[i]); }else if(arr[i].compareTo(map1.get(temp))>0){ result=result.subtract(map1.get(temp)); result=result.add(arr[i]); map1.put(temp, arr[i]); } } } sb.append(max).append(" ").append(result); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("buylow.out"))); System.out.println(sb.toString()); pw.println(sb.toString()); pw.close(); System.out.println("$:"+(System.currentTimeMillis()-start)); System.exit(0); }}public class buylow { public static void main(String[] args) throws Exception { problem2 p = new problem2(); p.solver(); }}
终于有会做的题了。。。
练了这么多DP,这种最基础的就是送了
比较讨厌的是要计算所有唯一的路径,就是在算总数的过程中,相同终点,并且相同长度的保留最长的
其实长的一定覆盖了短的。用了个吊hashmap强行写的
然后数据太大,必须要用BigInteger, 这就是java的好处了,估计C++的longlong过不去吧
0 0
- USACO 4.3.1 Buy Low, Buy Lower
- usaco 4.3.1 Buy Low, Buy Lower
- USACO 4.3 PROB Buy Low, Buy Lower
- Usaco 4.3.1 Buy Low, Buy Lower 详细解题报告
- usaco 4.3.1 Buy Low, Buy Lower DP + 高精度
- usaco training 4.3.1 Buy Low, Buy Lower 题解
- usaco 4.3 Buy Low, Buy Lower 动态规划
- USACO Section 4.3 Buy Low, Buy Lower - DP+大数加法
- usaco 4.3 Buy Low, Buy Lower(DP+大数)
- USACO 4.3 Buy Low, Buy Lower 动态规划
- usaco 4.3 Buy Low, Buy Lower 2010.8.5
- USACO-Section 4.3 Buy Low, Buy Lower (DP[LIS])
- USACO Buy Low, Buy Lower 解题报告
- Usaco 4.3.1 Buy Low, Buy Lower 逢低吸纳详细解题报告
- 【USACO题库】4.3.1 Buy Low, Buy Lower逢低吸纳
- C++——【USACO 4.3.1】——Buy Low, Buy Lower
- BUY LOW, BUY LOWER
- buy low buy lower
- BING : Binarized Normed Gradients for Objectness Estimation at 300fps 论文笔记
- Busybox init进程启动过程 (资料收集)
- 辟谷日记
- OC学习笔记之ARC
- 数据库实验代码备份
- usaco 4.3.1 Buy Low, Buy Lower
- Gephi统计算法
- java字符编码转换研究(转)
- 最简单的基于FFmpeg的音频编码器 PCM 转AAC
- mysql按中文首字母排序
- Java 中String的HashCode计算方法
- myeclipse10下修改默认字符为utf8
- 搜索智能提示suggestion,附近点搜索
- 越来越多地Windows Phone被西欧各国政府所采纳