import java.util.Scanner; import java.io.*; /* * Lab 2 * * Author: Robin Dawes * * Date: 20080918 * * Comment: The first required sum is computed explicitly, which takes O(n) time. Each subsequent * sum is computed in constant time. Since there are at most O(n^2) sums, the algorithm * runs in O(n^2) time. * */ public class Lab_2 { static int numAnalysts; static int numDays; static int minProfitPeriod; static int[] dayScores; static int currentScore; static int prevInitScore; public static void main(String[] argv) throws Exception { Scanner myScanner = new Scanner(new File("Lab_2_Data.txt")); numAnalysts = myScanner.nextInt(); numDays = myScanner.nextInt(); dayScores = new int[numDays]; minProfitPeriod = myScanner.nextInt(); for (int i = 0; i < numAnalysts; i++) { for (int j = 0; j < numDays; j++) dayScores[j] = myScanner.nextInt(); int startPos = 0; int endPos = minProfitPeriod - 1; int lastStartPos = numDays - minProfitPeriod; currentScore = 0; boolean safe = false; // examine the first feasible period (starting on day 0) for (int k = startPos; k <= startPos + minProfitPeriod; k++) currentScore += dayScores[k]; prevInitScore = currentScore; safe = currentScore > 0; // if necessary, deal with the longer periods starting on day 0 while ((!safe) && (endPos < numDays-1)) { endPos++; currentScore += dayScores[endPos]; safe = currentScore > 0; } // while // if necessary, deal with the periods starting on days other than 0 while ((!safe) && (startPos < lastStartPos)) { // move to the next start day startPos++; endPos = startPos + minProfitPeriod - 1; currentScore = prevInitScore - dayScores[startPos-1] + dayScores[endPos]; prevInitScore = currentScore; safe = currentScore > 0; // if necessary, work through the longer periods starting on this day while ((!safe) && (endPos < numDays - 1)) { endPos++; currentScore += dayScores[endPos]; safe = currentScore > 0; } // while } // while if (safe) System.out.println("Yes"); else System.out.println("No"); } // for } // main(String[]) } // clas Lab_2