공부/코딩

[알고리즘 스터디] 7주차 결산

sourceoftax 2025. 12. 15. 21:55

1116

#include <bits/stdc++.h>
using namespace std;

long long n, l, w, h;

bool can(double a) {
    return (long long)(l / a) * (long long)(w / a) * (long long)(h / a) >= n;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> l >> w >> h;

    double low = 0, high = max({l, w, h});
    
    for (int i = 0; i < 10000; i++) {  
        double mid = (low + high) / 2;
        if (can(mid))
            low = mid;
        else
            high = mid;
    }

    cout << fixed << setprecision(10) << low << "\n";
    return 0;
}

 

1117

#include <iostream>
#include <stack>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string word;
    cin >> word;

    int n;
    cin >> n;

    stack<char> left, right;

    for (char c : word) left.push(c);

    while (n--) {
        char cmd;
        cin >> cmd;
        
        if (cmd == 'L') {
            if (!left.empty()) {
                right.push(left.top());
                left.pop();
            }
        }
        else if (cmd == 'D') {
            if (!right.empty()) {
                left.push(right.top());
                right.pop();
            }
        }
        else if (cmd == 'B') {
            if (!left.empty()) left.pop();
        }
        else if (cmd == 'P') {
            char x;
            cin >> x;
            left.push(x);
        }
    }

    string result;
    while (!left.empty()) {
        right.push(left.top());
        left.pop();
    }
    while (!right.empty()) {
        result += right.top();
        right.pop();
    }

    cout << result;
}

 

1118

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
int num;
const int Max = 2 * 123456;
vector<bool> arr(Max, true);
arr[0] = arr[1] = false;

for (int i = 2; i * i < Max; i++){
if(arr[i]){
for (int j = i*i; j < Max; j += i){
arr[j] = false;}
				}
}

while (true){
cin >> num;
if(num == 0) break;

int count = 0;
for(int k = num+1; k < (num *2) +1;k++){
if(arr[k]) count++;
}
cout << count << "\n";
}


return 0;
}

 

1119

#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int num, newscore, p;
    cin >> num >> newscore >> p;

    vector<int> arr(num);   
    for (int i = 0; i < num; i++)
        cin >> arr[i];

    if (num == 0) {
        cout << 1;
        return 0;
    }

    if (num == p && newscore <= arr[num - 1]) {
        cout << -1;
        return 0;
    }

    int rank = 1;
    for (int i = 0; i < num; i++) {
        if (arr[i] > newscore) rank++;
        else break;
    }

    cout << rank;
    return 0;
}

 

1120

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>   
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int num;
    cin >> num;
    vector<long long> arr(num);
    for (int i = 0; i < num; i++)
        cin >> arr[i];

    vector<int> idx(num);
    iota(idx.begin(), idx.end(), 0);

    sort(idx.begin(), idx.end(), [&](int x, int y) {
        if (arr[x] == arr[y]) return x < y;
        return arr[x] < arr[y];
    });

    vector<int> p(num);

    for (int pos = 0; pos < num; pos++) {
        p[idx[pos]] = pos;
    }

    for (int i = 0; i < num; i++)
        cout << p[i] << " ";
    cout << "\n";

    return 0;
}

 

1121

num, s = map(int, input().split())
arr = list(map(int, input().split()))

left = 0
sum_val = 0
answer = float('inf')

for right in range(num):
    sum_val += arr[right]

    while sum_val >= s:
        answer = min(answer, right - left + 1)
        sum_val -= arr[left]
        left += 1

print(0 if answer == float('inf') else answer)