-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
43-gjsk132 #234
base: main
Are you sure you want to change the base?
43-gjsk132 #234
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ μ²μμ "λμ§ ν¬μΈν° 4κ°λ₯Ό κ°κ³ λμμΌ νλ?" μ΄ μκ°νλ€κ° μμ¬ λλ©μ΄λ 2μ€ forλ¬ΈμΌλ‘ naiveνκ² λλ¦¬κ³ μλ λλ©μ΄ 2κ°μ λν΄μ ν¬ν¬μΈν°λ₯Ό ꡴리λ λ°©μμΌλ‘ νμ΅λλ€.
κ·Έλ°λ° λ무 μ€λ 걸리λ κ΅°μ. μ§μμ νμ§λ§...
κ·Έλμ κ°λ₯ν λμ¬λ μ‘°ν©μ 미리 λ°°μ΄μ μ μ₯ν΄λ λ€, κ±°κΈ°μ ν¬ν¬μΈν°λ₯Ό λ리λ μμΌλ‘ κ³ μ³λ³΄λκΉ μκ°μ΄ ν λΉ¨λΌμ§λ€μ :)
λ³κ²½λ μ½λ
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct FSnowman
{
int Height;
int FirstSnowball;
int SecondSnowball;
FSnowman(int InHeight, int InFirstSnowball, int InSecondSnowball)
: Height(InHeight)
, FirstSnowball(InFirstSnowball)
, SecondSnowball(InSecondSnowball)
{}
bool operator<(const FSnowman& InSnowman) const
{
return this->Height < InSnowman.Height;
}
};
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int SnowballCount; cin >> SnowballCount;
vector<int> SnowballDiameters(SnowballCount); for(int& Diameter : SnowballDiameters) cin >> Diameter;
vector<FSnowman> Snowmans;
for(int i = 0; i < SnowballCount - 1; ++i)
{
for(int j = i + 1; j < SnowballCount; ++j)
{
Snowmans.emplace_back(SnowballDiameters[i] + SnowballDiameters[j], i, j);
}
}
sort(Snowmans.begin(), Snowmans.end());
int MinSnowmanHeightDifference = 1e9;
for(int Elsa = 0; Elsa < Snowmans.size() - 1; ++Elsa )
{
int Anna = Elsa + 1;
while(Anna < Snowmans.size()
&& (Snowmans[Elsa].FirstSnowball == Snowmans[Anna ].FirstSnowball
|| Snowmans[Elsa].FirstSnowball == Snowmans[Anna].SecondSnowball
|| Snowmans[Elsa].SecondSnowball == Snowmans[Anna].FirstSnowball
|| Snowmans[Elsa].SecondSnowball == Snowmans[Anna].SecondSnowball))
{
Anna++;
}
if(Anna < Snowmans.size())
{
MinSnowmanHeightDifference = min(MinSnowmanHeightDifference,
Snowmans[Anna].Height - Snowmans[Elsa].Height);
}
}
cout << MinSnowmanHeightDifference;
return 0;
}
|
||
target = snow[s1] + snow[s2] | ||
|
||
min_gap = target |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μκ±° μ΄κΈ°κ°μ μ΄λ° μμΌλ‘ μ€μ νλ©΄
4
1 2 3 999
answer: 995
output: 3
μ΄λ° μμΌλ‘ μΆλ ₯λ©λλΉ. μ΄κΈ°κ° μ€μ μ λ€μ κ³ λ―Όν΄λ³ΌκΉμ!?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€νΈ κ°μ¬ν¨λλΉ
min_gap = float('inf')
κ·Όλ° μ΄μ λλ©΄ μΉλͺ μ μΈλ° μ΄μΊ λ§μ·μ§ ν μΌ μΆκ°ν΄λ¬λΌμΉΌκΉμπ«
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄μ 곧 "20366λ² - κ°μ΄ λμ¬λ λ§λ€λ? λ¬Έμ κ° μ¬μ±μ λμμ΅λλ€. (μ¬μ±μ μ΄μ : λ°μ΄ν° μΆκ°)" 보λ κ±ΈκΉμ γ γ γ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€νΈ μ΄ λ¬Έμ μμ ν¬ν¬μΈν°λ μμͺ½ λμμ μ’νμ€λ€μ!!
μ λ²λ μ²λΌ ν λ°©ν₯μΌλ‘ μμ§μ΄λ ν¬ν¬μΈν° λ¬Έμ λ₯Ό μμ£Ό νμ΄μ μμͺ½ λμ μ΄λ»κ² νμμ§νλ©΄μ λλ¬κ±°λ Έμ΅λλ€ γ
γ
γ
π
μ½λ 보기
import java.io.*;
import java.util.*;
public class Main {
static int N;
static int snow[];
static int min = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
snow = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
snow[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(snow);
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int snowMan1 = snow[i] + snow[j];
int start = 0;
int end = N - 1;
while (start < end) {
if (start == i || start == j) {
start++;
continue;
}
if (end == i || end == j) {
end--;
continue;
}
int snowMan2 = snow[start] + snow[end];
min = Math.min(min, Math.abs(snowMan1 - snowMan2));
if (snowMan1 > snowMan2) start++;
else if (snowMan1 < snowMan2) end--;
else {
System.out.println(0);
return;
}
}
}
}
System.out.println(min);
}
}
π λ¬Έμ λ§ν¬
λ°±μ€ 20366 : κ°μ΄ λμ¬λ λ§λ€λ?
βοΈ μμλ μκ°
1μκ° 30λΆ
β¨ μλ μ½λ
π― λ¬Έμ μ΄ν΄
μ²μμλ 4κ°λ₯Ό λ€ κ³¨λΌμ€μΌνλ μΆμλλ°...
κ·Έλ₯ 2κ°λ 미리 골λΌμ targetμΌλ‘ μ νκ³ , λλ¨Έμ§ 2κ°λ₯Ό ν¬ν¬μΈν°λ‘ λλ©΄μ targetκ°κ³Ό κ°κΉμμ§λλ‘ νμλ€.
μ¬μ©ν λλ©μ΄λ μ°λ©΄ μλκΈ° λλ¬Έμ μ¬μ©ν λλ©μ΄μ κ°μ idxλ‘ κ°λ©΄ μ€ν΅ν΄μ€λ€.
πμ½λ μ€λͺ
μ£Όμ μ½λ
κ°μ₯ ν΅μ¬μ΄ λλ ν¬ν¬μΈν° μ½λμ λλ€.
2κ°λ‘ μ μ ν κ°μ block1, block2λ‘ μ νκ³ , ν΄λΉ μμΉμ λλ©μ΄ μ§λ¦μ ν©μ targetμΌλ‘ μ§μ ν΄μ€λλ€.
μ΅μ μ°¨μ΄λ₯Ό targetμΌλ‘ ν΄λκ³ , startμ endλ‘ λ°λ³΅λ¬Έμ λλ €μ€λλ€.
λ§μ½ block1κ³Ό block2λ κ²ΉμΉλ€λ©΄ startλ©΄ +1, endλ©΄ -1μ ν΄μ€λλ€.
μ΄νμλ startμ endλ‘ λ§λ λμ¬λμ ν€λ₯Ό targetκ³Ό λΉκ΅ν΄μ λ°λ³΅λ¬Έμ λλ €μ£Όλ©΄ λ©λλ€.
λλκ³ λλ©΄ κ°μ₯ μμ ν€ μ°¨μ΄λ₯Ό μΆλ ₯ν΄μ€λλ€.
μ ν¬ν¬μΈν° μ½λλ₯Ό μ΄μ©νμ¬
2μ€ λ°λ³΅λ¬ΈμΌλ‘ κ³ μ νλ €λ 2κ°μ λλ©μ΄λ₯Ό μ ν΄μ£Όκ³ , μ΅μκ°μ answerμ μ ν΄μ€λλ€.
μκ°μ μ€μ΄κΈ° μν΄ answerμ΄ 0μ΄ λλ κ°μ΄ λμ€λ©΄ λ°λ³΅μ μ€λ¨νκ³ , κ²°κ³Όλ₯Ό μΆλ ₯ν©λλ€.
μ 체 μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μ΅κ·Όμ ν¬ν¬μΈν° λ¬Έμ λ₯Ό λ§μ΄ μ νλλ°, μμ£Ό μ νμ΄λ΄μ ν λ² νμ΄λ΄ λλ΄
κ·Έλλ μμ§ μμ©νλ €κ³ νλκΉ μ½κ° μ΄λ ΅λ€μ νμ π€