AtCoder Beginner Contest 167 B Problem "Easy Linear Programming" Explanation (Python3, C ++, Java)

AtCoder Beginner Contest 167 B I will explain the problem "Easy Linear Programming".

Problem URL: https://atcoder.jp/contests/abc167/tasks/abc167_b

Problem summary

There are $ A $ cards with $ 1 $, $ B $ cards with $ 0 $, and $ C $ cards with $ -1 $. Answer some of the maximum possible sums of the numbers written on the cards you took when you picked just $ K $ from these cards.

Constraint

・ All inputs are integers ・ $ 0 \ leq A, B, C $ ・ $ 1 \ leq K \ leq A + B + C \ leq 2 × 10 ^ 9 $

Commentary

To make the maximum value as large as possible </ b>, the card is taken according to the value of $ K $ as follows.

  1. For $ K \ leq A $ All you have to do is select and take a card with $ 1 $ written on it.

  2. If $ A <K \ leq A + B $ After performing the method shown in 1., select the card with the remaining $ (K-A) $ $ 0 $ written on it.

  3. If $ A + B <K \ leq A + B + C $ After performing the method shown in 2., select the card with the remaining $ (K-A-B) $ $ -1 $ written on it.

In case of 1. The answer is $ K $ In case 2. The answer is $ A $ In case of 3, the answer is $ A- (K-A-B) $ It will be.

You can change the output of the answer depending on the value of $ K $.

In addition, there are restrictions A+B+C \leq 2×10^9 Although it is The range of numbers that can be represented by a general ʻint type (C ++ or Java) <font color = "red"> $ -2 ^ {31} $ ~ $ 2 ^ {31} -1 $ </ font>, Since $ 2 ^ {31} = 21474883647 $, Within the constraints, it's okay to define $ K $ as an ʻint type.

Below are examples of solutions in Python3, C ++, and Java.

Example of answer for each language

Example solution in Python3

{ABC167B.py}


A,B,C,K = map(int,input().split())
if (K <= A):
  print(K)
elif (A < K <= A+B):
  print(A)
else:
  print(A-(K-A-B))
Example solution in C ++

{ABC167B.cpp}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b,c,k;
  cin >> a >> b >> c >> k;
  if (k <= a){
    cout << k << endl;
  }else if (k <= a+b){
    cout << a << endl;
  }else{
    cout << a-(k-a-b) << endl;
  }
}
Java answer example

{ABC167B.java}


import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int k = scan.nextInt();
    if (k <= a){
      System.out.println(k);
    }else if (k <= a+b){
      System.out.println(a);
    }else{
      System.out.println(a-(k-a-b));
    }
  }
}

Recommended Posts

AtCoder Beginner Contest 167 B Problem "Easy Linear Programming" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 174 B Problem "Distance" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 177 B Problem "Substring" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 169 B Problem "Multiplication 2" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 175 B Problem "Making Triangle" Explanation (C ++, Python3, Java)
AtCoder Beginner Contest 176 C Problem "Step" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 173 B Problem "Judge Status Summary" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (Python3, C ++, Java)
AtCoder Beginner Contest 167 A Problem "Registration" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 170 A Problem "Five Variables" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 169 A Explanation of Problem "Multiplication 1" (Python3, C ++, Java)
AtCoder Beginner Contest 176 A Explanation of problem "Takoyaki" (Python3, C ++, Java)
AtCoder Beginner Contest 175 A Problem "Rainy Season" Explanation (C ++, Python3, Java)
AtCoder Beginner Contest 177 A Problem "Don't be late" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 177 C Problem "Sum of product of pairs" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 174 C Problem (Python)
AtCoder Beginner Contest 165 A Problem "We Love Golf" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest # 002 C Problem
Atcoder Beginner Contest A, B Input summary Python that tends to be a problem
Atcoder Acing Programming Contest Python
AtCoder Regular Contest # 002 C Problem
Atcoder Beginner Contest 152 Kiroku (python)
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
ABC127 A, B, C Explanation (python)
ABC126 A, B, C Explanation (python)
[Python] [Explanation] AtCoder Typical DP Contest: A Contest
Python beginner Atcoder memo @ KEYENCE 2020, ABC problem
AtCoder Beginner Contest: D Question Answers Python
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
AtCoder Beginner Contest 177
AtCoder Beginner Contest 179
[Python] Sumitomo Mitsui Trust Bank Programming Contest 2019 C (How to use DP) [AtCoder]
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
AtCoder Beginner Contest 172
AtCoder Beginner Contest 180
AtCoder Beginner Contest 173
Atcoder Beginner Contest 153
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
AtCoder Beginner Contest 177 Problem C I tried to find out why it was wrong
AtCoder Beginner Contest 152 Review
AtCoder Beginner Contest 181 Note
AtCoder Beginner Contest 187 Note
AtCoder Beginner Contest 160 Review
AtCoder Beginner Contest 178 Review
AtCoder Beginner Contest 180 Note
AtCoder Beginner Contest 166 Review
AtCoder Beginner Contest 167 Review
AtCoder Beginner Contest 164 Review
AtCoder Beginner Contest 169 Review