-Baekjoon Online Judging 11399 - ATM-


[Probs]

https://www.acmicpc.net/problem/11399


[Solve]

그리디 알고리즘...이라고 하기도 민망할 정도로 쉬운 문제였다.

Sorting을 하는 방법과 Priority-Queue를 이용하는 방법이 있다.

하지만 본인의 경우 STL의 multiset이라는 컨테이너를 사용해서 풀었다.

multiset의 경우 기존의 set에서 중복 원소를 허용하는 것으로, insert시 정렬이 가능하다.

priority_queue보다 performance가 우수하며, iteration이 가능해 Range-based for loop에도 사용할 수 있다.


[Code]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <set>
 
int main(){
    int num, tmp, ret = 0;
    std::multiset<intstd::less<int>> vSet;
    std::cin >> num;
    for(int i = 0; i < num; ++i){
        std::cin >> tmp;
        vSet.insert(tmp);
    }
    tmp = 0;
    for(auto x : vSet) {
        tmp = x + tmp;
        ret += tmp;
    }
    std::cout << ret;
}
 
cs

Posted by RevDev
,

-Callback implementation with STL (Modern C++)-



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "global.h"
#include "core/interface.h"
 
typedef std::function<void()> Callback;
 
int main(int argc, char** argv)
{
    std::map<std::string, Callback> mapCallback;
    mapCallback.insert(std::pair<std::string, Callback>("open"std::bind(MainInterface::setupUnkFile)));
    mapCallback.insert(std::pair<std::string, Callback>("procview"std::bind(MainInterface::logviewCurrentProc)));
    mapCallback.insert(std::pair<std::string, Callback>("history"std::bind(MainInterface::logviewHistoryProc)));
    mapCallback.insert(std::pair<std::string, Callback>("vmstatus"std::bind(MainInterface::logviewCurrentStat)));
    mapCallback.insert(std::pair<std::string, Callback>("procmgr"std::bind(MainInterface::ctrlProcMgr)));
    mapCallback.insert(std::pair<std::string, Callback>("imexport"std::bind(MainInterface::ctrlImExport)));
    mapCallback.insert(std::pair<std::string, Callback>("exit"std::bind([](){std::exit(0);})));
 
    if (argc > 1)
    {
        std::string strArgv = argv[1];
        auto it = mapCallback.find(strArgv);
        if (it != mapCallback.end())
            it->second();
    }
 
    else MainInterface::setupInterface();
}
 
cs



Posted by RevDev
,

-Thread-Safe Singleton Template (Modern C++)-



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <thread>
#include <mutex>
 
template <class T>
class Singleton {
protected:
    Singleton() {};
    ~Singleton() {};
    Singleton(const Singleton& other) {};
 
private:
    static std::once_flag chkSingle;
    static std::shared_ptr<T> pInstance;
 
    static void destroy() {
        if (pInstance != nullptr) {
            pInstance.reset();
            pInstance = nullptr;
        }
    }
 
public:
    static T& getInstance() {
        std::call_once(chkSingle, []()
        {
            pInstance.reset(new T());
            std::atexit(destroy);
        });
        return *(pInstance.get());
    }
};
 
template <class T> std::shared_ptr<T> Singleton <T>::pInstance = nullptr;
template <class T> std::once_flag Singleton <T>::chkSingle;
cs



Posted by RevDev
,

customio.inc

.set stdin, 0x00
.set stdout, 0x01

.set syscall_read, 0x3
.set syscall_write, 0x4

.text
.globl strlen
.globl strcpy
.globl puts
.globl gets

strlen:
    push %ebp
    mov %esp, %ebp
    push %esi
    push %ecx
    xor %ecx, %ecx
    mov 0x8(%ebp), %esi
    .strlen_lp:
        movb (%esi), %al
        cmpb $0x0, %al
        je .strlen_lp_fin
        inc %esi
        inc %ecx
        jmp .strlen_lp
    .strlen_lp_fin:
    mov %ecx, %eax
    pop %ecx
    pop %esi
    leave
    ret $0x4

strcpy:
    push %ebp
    mov %esp, %ebp
    push %edi
    push %esi
    push %ecx
    xor %ecx, %ecx
    mov 0x8(%ebp), %edi
    mov 0xc(%ebp), %esi
    .strcmp_lp:
        cmpb $0x0, (%esi)
        movb $0x0, (%edi)
        je .strcmp_lp_fin
        movb (%esi), %al
        movb %al, (%edi)
        inc %esi
        inc %edi
        inc %ecx
        jmp .strcmp_lp
    .strcmp_lp_fin:
    mov %ecx, %eax
    pop %ecx
    pop %esi
    pop %edi
    leave
    ret $0x4

puts:
    push %ebp
    mov %esp, %ebp
    push %ebx
    push %ecx
    push %edx
    mov 0x8(%ebp), %ecx
    push %ecx
    call strlen
    mov %eax, %edx
    mov $stdout, %ebx
    mov $syscall_write, %eax
    int $0x80
    mov %ecx, %eax
    pop %edx
    pop %ecx
    pop %ebx
    leave
    ret $0x4

gets:
    push %ebp
    mov %esp, %ebp
    sub $0x1000, %esp
    push %esi
    push %ebx
    push %ecx
    push %edx
    mov $0x1000, %edx
    lea -0x1000(%ebp), %ecx
    mov $stdin, %ebx
    mov $syscall_read, %eax
    int $0x80
    mov 0x8(%ebp), %eax
    push %ecx
    push %eax
    call strcpy
    pop %edx
    pop %ecx
    pop %ebx
    pop %esi
    leave
    ret $0x4


main.s

.text
.include "customio.inc"
.globl main

main:
    push %ebp
    mov %esp, %ebp
    push %edx
    sub $0x100, %esp
    lea -0x100(%ebp), %edx
    push $string
    call puts
    push %edx
    call gets
    push %edx
    call puts
    xor %eax, %eax
    pop %edx
    leave
    ret

.section .rodata
string:
    .string "Input : "
Posted by RevDev
,