import sysdef main(): data = sys.stdin.read().split() n, B, g, w, k = (int(v) for v in data[:5]) prices = [int(v) for v in data[5:5 + n]] holding = False cost = 0 # 산 값 high = 0 # 산 날부터 지금까지 가장 높았던 값 rest = 0 # 남은 쉬는 날 수 profit = 0 out = [] for day, p in enumerate(prices, start=1): if holding: if p >= cost + g: # 규칙 1: 이익 실현 out.append(f"sell {day} {p}") profit += p - cost holding = False elif p <= high - w: # 규칙 2: 고점에서 w 이상 내려옴 out.append(f"sell {day} {p}") profit += p - cost holding = False if p < cost: # 손해를 보고 팔았을 때만 쉰다 rest = k else: # 규칙 3: 그대로 들고 간다 high = max(high, p) else: # 판 날에는 이쪽으로 오지 않는다 if rest > 0: # 규칙 4: 쉬는 날 rest -= 1 elif p <= B: # 규칙 5: 산다 out.append(f"buy {day} {p}") holding = True cost = p high = p out.append(f"{profit} {1 if holding else 0}") print("\n".join(out))main()
JavaScriptmain.js · 42 行
const data = require("fs").readFileSync(0, "utf8").split(/\s+/).filter(Boolean).map(Number);const [n, B, g, w, k] = data;let holding = false;let cost = 0; // 산 값let high = 0; // 산 날부터 지금까지 가장 높았던 값let rest = 0; // 남은 쉬는 날 수let profit = 0;const out = [];for (let day = 1; day <= n; day++) { const p = data[4 + day]; if (holding) { if (p >= cost + g) { // 규칙 1: 이익 실현 out.push(`sell ${day} ${p}`); profit += p - cost; holding = false; } else if (p <= high - w) { // 규칙 2: 고점에서 w 이상 내려옴 out.push(`sell ${day} ${p}`); profit += p - cost; holding = false; if (p < cost) rest = k; // 손해를 보고 팔았을 때만 쉰다 } else { // 규칙 3: 그대로 들고 간다 high = Math.max(high, p); } } else { // 판 날에는 이쪽으로 오지 않는다 if (rest > 0) { rest--; // 규칙 4: 쉬는 날 } else if (p <= B) { // 규칙 5: 산다 out.push(`buy ${day} ${p}`); holding = true; cost = p; high = p; } }}out.push(`${profit} ${holding ? 1 : 0}`);console.log(out.join("\n"));
TypeScriptmain.ts · 43 行
import { readFileSync } from "node:fs";const data: number[] = readFileSync(0, "utf8").split(/\s+/).filter(Boolean).map(Number);const [n, B, g, w, k] = data;let holding = false;let cost = 0; // 산 값let high = 0; // 산 날부터 지금까지 가장 높았던 값let rest = 0; // 남은 쉬는 날 수let profit = 0;const out: string[] = [];for (let day = 1; day <= n; day++) { const p = data[4 + day]; if (holding) { if (p >= cost + g) { // 규칙 1: 이익 실현 out.push(`sell ${day} ${p}`); profit += p - cost; holding = false; } else if (p <= high - w) { // 규칙 2: 고점에서 w 이상 내려옴 out.push(`sell ${day} ${p}`); profit += p - cost; holding = false; if (p < cost) rest = k; // 손해를 보고 팔았을 때만 쉰다 } else { // 규칙 3: 그대로 들고 간다 high = Math.max(high, p); } } else { // 판 날에는 이쪽으로 오지 않는다 if (rest > 0) { rest--; // 규칙 4: 쉬는 날 } else if (p <= B) { // 규칙 5: 산다 out.push(`buy ${day} ${p}`); holding = true; cost = p; high = p; } }}out.push(`${profit} ${holding ? 1 : 0}`);console.log(out.join("\n"));
JavaMain.java · 51 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.StreamTokenizer;public class Main { public static void main(String[] args) throws Exception { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int[] head = new int[5]; for (int i = 0; i < 5; i++) { in.nextToken(); head[i] = (int) in.nval; } int n = head[0], B = head[1], g = head[2], w = head[3], k = head[4]; boolean holding = false; int cost = 0; // 산 값 int high = 0; // 산 날부터 지금까지 가장 높았던 값 int rest = 0; // 남은 쉬는 날 수 int profit = 0; StringBuilder out = new StringBuilder(); for (int day = 1; day <= n; day++) { in.nextToken(); int p = (int) in.nval; if (holding) { if (p >= cost + g) { // 규칙 1: 이익 실현 out.append("sell ").append(day).append(' ').append(p).append('\n'); profit += p - cost; holding = false; } else if (p <= high - w) { // 규칙 2: 고점에서 w 이상 내려옴 out.append("sell ").append(day).append(' ').append(p).append('\n'); profit += p - cost; holding = false; if (p < cost) rest = k; // 손해를 보고 팔았을 때만 쉰다 } else { // 규칙 3: 그대로 들고 간다 high = Math.max(high, p); } } else { // 판 날에는 이쪽으로 오지 않는다 if (rest > 0) { // 규칙 4: 쉬는 날 rest--; } else if (p <= B) { // 규칙 5: 산다 out.append("buy ").append(day).append(' ').append(p).append('\n'); holding = true; cost = p; high = p; } } } out.append(profit).append(' ').append(holding ? 1 : 0).append('\n'); System.out.print(out); }}
C#Main.cs · 58 行
using System;using System.Text;class Program{ static void Main() { string[] data = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries); int n = int.Parse(data[0]), B = int.Parse(data[1]), g = int.Parse(data[2]), w = int.Parse(data[3]), k = int.Parse(data[4]); bool holding = false; int cost = 0; // 산 값 int high = 0; // 산 날부터 지금까지 가장 높았던 값 int rest = 0; // 남은 쉬는 날 수 int profit = 0; var sb = new StringBuilder(); for (int day = 1; day <= n; day++) { int p = int.Parse(data[4 + day]); if (holding) { if (p >= cost + g) // 규칙 1: 이익 실현 { sb.Append("sell ").Append(day).Append(' ').Append(p).Append('\n'); profit += p - cost; holding = false; } else if (p <= high - w) // 규칙 2: 고점에서 w 이상 내려옴 { sb.Append("sell ").Append(day).Append(' ').Append(p).Append('\n'); profit += p - cost; holding = false; if (p < cost) rest = k; // 손해를 보고 팔았을 때만 쉰다 } else // 규칙 3: 그대로 들고 간다 { high = Math.Max(high, p); } } else // 판 날에는 이쪽으로 오지 않는다 { if (rest > 0) // 규칙 4: 쉬는 날 { rest--; } else if (p <= B) // 규칙 5: 산다 { sb.Append("buy ").Append(day).Append(' ').Append(p).Append('\n'); holding = true; cost = p; high = p; } } } sb.Append(profit).Append(' ').Append(holding ? 1 : 0).Append('\n'); Console.Write(sb); }}
C++main.cpp · 43 行
#include <algorithm>#include <iostream>int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, B, g, w, k; std::cin >> n >> B >> g >> w >> k; bool holding = false; int cost = 0; // 산 값 int high = 0; // 산 날부터 지금까지 가장 높았던 값 int rest = 0; // 남은 쉬는 날 수 int profit = 0; for (int day = 1; day <= n; day++) { int p; std::cin >> p; if (holding) { if (p >= cost + g) { // 규칙 1: 이익 실현 std::cout << "sell " << day << ' ' << p << '\n'; profit += p - cost; holding = false; } else if (p <= high - w) { // 규칙 2: 고점에서 w 이상 내려옴 std::cout << "sell " << day << ' ' << p << '\n'; profit += p - cost; holding = false; if (p < cost) rest = k; // 손해를 보고 팔았을 때만 쉰다 } else { // 규칙 3: 그대로 들고 간다 high = std::max(high, p); } } else { // 판 날에는 이쪽으로 오지 않는다 if (rest > 0) { // 규칙 4: 쉬는 날 rest--; } else if (p <= B) { // 규칙 5: 산다 std::cout << "buy " << day << ' ' << p << '\n'; holding = true; cost = p; high = p; } } } std::cout << profit << ' ' << (holding ? 1 : 0) << '\n';}
Cmain.c · 41 行
#include <stdio.h>int main(void) { int n, B, g, w, k; scanf("%d %d %d %d %d", &n, &B, &g, &w, &k); int holding = 0; int cost = 0; /* 산 값 */ int high = 0; /* 산 날부터 지금까지 가장 높았던 값 */ int rest = 0; /* 남은 쉬는 날 수 */ int profit = 0; for (int day = 1; day <= n; day++) { int p; scanf("%d", &p); if (holding) { if (p >= cost + g) { /* 규칙 1: 이익 실현 */ printf("sell %d %d\n", day, p); profit += p - cost; holding = 0; } else if (p <= high - w) { /* 규칙 2: 고점에서 w 이상 내려옴 */ printf("sell %d %d\n", day, p); profit += p - cost; holding = 0; if (p < cost) rest = k; /* 손해를 보고 팔았을 때만 쉰다 */ } else { /* 규칙 3: 그대로 들고 간다 */ if (p > high) high = p; } } else { /* 판 날에는 이쪽으로 오지 않는다 */ if (rest > 0) { /* 규칙 4: 쉬는 날 */ rest--; } else if (p <= B) { /* 규칙 5: 산다 */ printf("buy %d %d\n", day, p); holding = 1; cost = p; high = p; } } } printf("%d %d\n", profit, holding); return 0;}