Code Ja:Candies(Kickstart Round D 2018)
题目链接:
https://code.google.com/codejam/contest/6364486/dashboard
Problem
Supervin loves to eat candies. Today, his favorite candy shop is offering N candies, which are arranged in a line. The i-th candy in the line (counting starting from 1) has a sweetness level Si. Note that the sweetness level of a candy might be negative, which means the candy tastes bitter.
Supervin likes to eat sweet candies. However, candies with a combined sweetness level of more than D would be too much sweetness even for him. Supervin also realises that a candy with an odd sweetness level is "odd", and he does not want to eat more than O odd candies. In other words, an odd candy is a candy with a sweetness level that is not evenly divisible by 2. Additionally, since Supervin is in a rush, he can only eat a single contiguous subset of candies.
Therefore, he wants to eat a contiguous non-empty subset of candies in which there are at most O odd candies and the total sweetness level is maximized, but not more than D. Help Supervin to determine the maximum total sweetness level he can get, or returnIMPOSSIBLE if there is no contiguous subset satisfying these constraints.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case contains two lines. The first line contains three integers N, O, and D, as described above. The second line contains seven integers X1, X2, A, B, C, M, L; these values are used to generate the values Si, as follows:
We define:
- Xi = (A × Xi - 1 + B × Xi - 2 + C) modulo M, for i = 3 to N.
- Si = Xi + L, for i = 1 to N.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum total sweetness level Supervin can get, or IMPOSSIBLE if there is no possible contiguous subset satisfying the problem constraints.
Limits
1 ≤ T ≤ 100.
2 ≤ N ≤ 5 × 105.
0 ≤ O ≤ N.
-1015 ≤ D ≤ 1015.
0 ≤ X1, X2, A, B, C ≤ 109.
1 ≤ M ≤ 109.
Small dataset
L = 0.
Large dataset
-5 × 108 ≤ L ≤ 0.
Input 5 6 1 1000000000000000 1 1 1 1 0 100 0 6 1 -100 1 1 1 1 0 100 0 10 1 8 4 3 4 1 5 20 -10 10 2 8 4 3 4 1 5 20 -10 10 1 8 4 3 4 1 5 20 -19 Output Case #1: 13 Case #2: IMPOSSIBLE Case #3: 7 Case #4: 8 Case #5: -5
题目大概:n个糖果、每个糖果的甜度是Si(i from 1 to n);求连续子集的糖果总甜度最大,但是总甜度不可超过D,并且子集中的奇数甜度不能超过O
并且Si的
Xi = (A × Xi - 1 + B × Xi - 2 + C) modulo M, for i = 3 to N.
Si = Xi + L, for i = 1 to N.
package interview.tree.interview; import java.util.*; import java.io.*; /** * Created by BUAA on 2018-07-30. */ public class Candies { static private int N; static private long O; static private long D; static private long X1; static private long X2; static private long A; static private long B; static private long C; static private long M; static private long L; static List<Long> sweetList = new ArrayList<Long>(); // oodlist :0 not odd ;1 for odd; static List<Long> oddList = new ArrayList<Long>(); public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. for (int i = 1; i <= t; ++i) { N = in.nextInt(); O = in.nextLong(); D = in.nextLong(); X1 = in.nextLong(); X2 = in.nextLong(); A = in.nextLong(); B = in.nextLong(); C = in.nextLong(); M = in.nextLong(); L = in.nextLong(); String result = process(); System.out.println("Case #" + i + ": " + result); } } public static String process() { int windowsStart = 1; int windowsEnd= 0 ; int windowsStartLater = 1; int windowsEndLater = 0 ; long maxSweetSum = (long)(0 - 2e63); int oddSum = 0; long currentWindowSum = 0; sweetList.clear();; oddList.clear(); // 将sweet添加到list中 // set odd list for (int i = 1;i <= N; ++i){ long currentSweet = 0; if (i == 1) { currentSweet = X1 +L; }else if (i == 2) { currentSweet = X2 +L; }else { currentSweet = ( ( (A%M * ((sweetList.get(sweetList.size() - 1)%M - L%M)%M))%M + (B%M * ((sweetList.get(sweetList.size() - 2)%M - L%M))%M)%M + C%M ) )%M +L; } sweetList.add(currentSweet); oddList.add(currentSweet & 1); currentWindowSum += currentSweet; oddSum += (currentSweet & 1); windowsEnd++; if (currentWindowSum > D || oddSum> O ) { while((currentWindowSum > D || oddSum> O) && windowsStart <= i) { if (sweetList.size() > 1){ currentWindowSum -= sweetList.get(windowsStart - 1); oddSum -= oddList.get(windowsStart - 1); }else { currentWindowSum -= sweetList.get(0); oddSum -= oddList.get(0); } windowsStart++; } } if (currentWindowSum > maxSweetSum){ maxSweetSum = currentWindowSum; windowsStartLater = windowsStart; windowsEndLater = i; } //maxSweetSum = Math.max(maxSweetSum,currentWindowSum); } /*for (Long testSweet: sweetList){ System.out.print(testSweet + "\t"); } System.out.println(); for (Long testODD: oddList){ System.out.print(testODD+ "\t"); } System.out.println(); System.out.println(windowsStartLater); System.out.println(windowsEndLater);*/ if (windowsStart > windowsEnd) return "IMPOSSIBLE"; return Long.toString(maxSweetSum) ; } }
测试运行,小数据结果可以一次性提交Correct,但是大数据暂未通过,实现中对暂时想到的可能导致溢出的数据做了处理。

浙公网安备 33010602011771号