Cod sursa(job #2589751)

Utilizator benisavuSavu Beniamin benisavu Data 26 martie 2020 20:14:32
Problema Datorii Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 2.95 kb
package com.beniamin.savu.datorii;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Hello world!
 *
 */
public class Main {
	
	/**
	 * Code to detect when the father iterogates Gigel.
	 */
	private static final int FATHER_INTEROGATION_OPERATION = 1;
	
	public static void main(String[] args) {
		FileReader fileReader = new FileReader();
		fileReader.open("datorii.in");
		
		// Read "n" and "m"
		int n = fileReader.nexInt();
		int m = fileReader.nexInt();
		
		// Read A1, A2, .... An
		int[] customersDebts = new int[n];
		int totalCustomerDebt = 0;
		for (int i = 0; i<n; i++) {
			customersDebts[i] = fileReader.nexInt();
			totalCustomerDebt += customersDebts[i];
		}
		
		StringBuilder result = new StringBuilder();
		for (int i = 0; i<m; i++) {
			int query = fileReader.nexInt();
			if(query == FATHER_INTEROGATION_OPERATION) {
				int dayStart = fileReader.nexInt();
				int dayEnd = fileReader.nexInt();
				if(dayEnd - dayStart + 1 == m) {
					result.append(totalCustomerDebt).append("\n");
				} else if (dayEnd - dayStart + 1 < m/2) {
					int totalDebt = 0;
					for(int j = dayStart - 1; j<dayEnd; j++) {
						totalDebt += customersDebts[j];
					}
					result.append(totalDebt).append("\n");
				} else {
					int totalDebt = 0;
					for(int j = 0; j<dayStart-1; j++) {
						totalDebt += customersDebts[j];
					}
					if(dayEnd != m) {
						for(int j = dayEnd; j<m; j++) {
							totalDebt += customersDebts[j];
						}
					}
					result.append(totalCustomerDebt - totalDebt).append("\n");
				}
			} else {
				int day = fileReader.nexInt() - 1;
				int amountPaid = fileReader.nexInt();
				customersDebts[day] -= amountPaid;
				totalCustomerDebt -= amountPaid;
			}
		}
		fileReader.close();
		FileWriter.write(result.toString(), "datorii.out");
	}
}

/**
 * Reads the content of a file.
 * 
 * @author benis
 *
 */
class FileReader {

	/**
	 * The reader.
	 */
	private Scanner reader;
	
	/**
	 * Reads the contents of the file.
	 */
	public void open(String filePath) {
		try {
			reader = new Scanner(new FileInputStream(filePath));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
	}
	
	/**
	 * Reads the next integer.
	 * 
	 * @return The integer.
	 */
	public int nexInt() {
		return reader.nextInt();
	}
	
	/**
	 * Closes the connection with the file.
	 */
	public void close() {
		reader.close();
	}

}

/**
 * Writes content to a file.
 * 
 * @author benis
 *
 */
class FileWriter {

	/**
	 * Writes the given content to the given file.
	 *
	 * @param content The content to write.
	 * @param filePath The file path
	 */
	public static void write(String content, String filePath) {
		try (PrintWriter writer = new PrintWriter(filePath)) {
			writer.write(content + "\n");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}