Efficient Interval Merging for Range Exclusion Calculations

Problem A: Textbook Availability Decision


#include<iostream>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int regular, extra, discount;
    cin >> regular >> extra >> discount;
    
    double direct_cost = regular + extra * 0.5;
    double discounted_cost = (regular + extra) * (100 - discount) / 100.0;
    
    cout << (discounted_cost < direct_cost ? "Through school" : "By myself");
    
    return 0;
}

Problem B: Sequence Bit Analysis


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

int main() {
    int n;
    cin >> n;
    vector<int> values(n);
    
    for(int i = 0; i < n; i++) {
        cin >> values[i];
        values[i] = abs(values[i]);
    }
    
    int result = 0;
    
    for(int num : values) {
        int ones = 0, zeros = 0;
        
        while(num > 0) {
            if(num % 2 == 1) ones++;
            else zeros++;
            num /= 2;
        }
        
        result += (ones > zeros) ? 1 : -1;
    }
    
    cout << result << endl;
    return 0;
}

Problem C: Subsrting Pattern Matching


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

int main() {
    int test_cases;
    cin >> test_cases;
    
    while(test_cases--) {
        string input;
        cin >> input;
        string target = "NowCoder";
        int match_index = 0;
        bool found = false;
        
        for(char c : input) {
            if(c == target[match_index]) {
                match_index++;
                if(match_index == target.length()) {
                    cout << "QAK" << endl;
                    found = true;
                    break;
                }
            }
        }
        
        if(!found) cout << "QIE" << endl;
    }
    
    return 0;
}

Problem D: Name Subsequence Counting


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

int main() {
    int name_count, cute_count;
    cin >> name_count >> cute_count;
    
    vector<string> names(name_count), cute_words(cute_count);
    
    for(int i = 0; i < name_count; i++) cin >> names[i];
    for(int i = 0; i < cute_count; i++) cin >> cute_words[i];
    
    for(string name : names) {
        int count = 0;
        
        for(string word : cute_words) {
            int word_index = 0;
            
            for(char c : name) {
                if(c == word[word_index]) {
                    word_index++;
                    if(word_index == word.length()) {
                        count++;
                        break;
                    }
                }
            }
        }
        
        cout << count << endl;
    }
    
    return 0;
}

Problem E: Interval Merging for Renge Exclusion


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

int main() {
    int total, interval_count;
    cin >> total >> interval_count;
    
    vector<pair<int, int>> intervals;
    
    for(int i = 0; i < interval_count; i++) {
        int start, end;
        cin >> start >> end;
        intervals.push_back({start, end});
    }
    
    sort(intervals.begin(), intervals.end());
    
    int current_start = intervals[0].first;
    int current_end = intervals[0].second;
    int result = total + 1;
    
    for(auto interval : intervals) {
        if(interval.first <= current_end) {
            current_start = min(current_start, interval.first);
            current_end = max(current_end, interval.second);
        } else {
            result -= current_end - current_start + 1;
            current_start = interval.first;
            current_end = interval.second;
        }
    }
    
    result -= current_end - current_start + 1;
    cout << result << endl;
    
    return 0;
}

Problem F: Sleep Duration Calculation


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

int main() {
    int sleep_hour, sleep_min, wake_hour, wake_min;
    char sleep_period, wake_period;
    int expected_hours, expected_minutes;
    
    scanf("%d:%d %c.m", &sleep_hour, &sleep_min, &sleep_period);
    scanf("%d:%d %c.m", &wake_hour, &wake_min, &wake_period);
    scanf("%dh%dmin", &expected_hours, &expected_minutes);
    
    int sleep_minutes, wake_minutes;
    
    if(sleep_period == 'p') sleep_minutes = sleep_hour * 60 + sleep_min + 12 * 60;
    else sleep_minutes = sleep_hour * 60 + sleep_min;
    
    if(wake_period == 'p') wake_minutes = wake_hour * 60 + wake_min + 12 * 60;
    else wake_minutes = wake_hour * 60 + wake_min;
    
    int actual_minutes = wake_minutes - sleep_minutes;
    if(actual_minutes < 0) actual_minutes += 24 * 60;
    
    int expected_total = expected_hours * 60 + expected_minutes;
    
    if(actual_minutes == expected_total) {
        cout << "YES" << endl;
    } else {
        int actual_hours = actual_minutes / 60;
        actual_minutes %= 60;
        if(actual_hours < 0) {
            actual_hours += 24;
            actual_minutes = (actual_minutes + 60) % 60;
        }
        cout << "NO" << endl;
        printf("%dh%dmin\n", actual_hours, actual_minutes);
    }
    
    return 0;
}

Problem H: Inversion Parity Analysis


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

int main() {
    int n;
    cin >> n;
    vector<int> arr(n);
    
    for(int i = 0; i < n; i++) cin >> arr[i];
    
    int inversion_count = 0;
    
    for(int i = 0; i < n; i++) {
        for(int j = i + 1; j < n; j++) {
            if(arr[i] > arr[j]) inversion_count++;
        }
    }
    
    int queries;
    cin >> queries;
    
    while(queries--) {
        int left, right;
        cin >> left >> right;
        int swap_count = (right - left + 1) / 2;
        
        bool current_odd = inversion_count % 2 == 1;
        bool swap_odd = swap_count % 2 == 1;
        
        if(current_odd == swap_odd) {
            cout << "like" << endl;
        } else {
            cout << "dislike" << endl;
            inversion_count++;
        }
    }
    
    return 0;
}

Tags: C++ algorithm interval-merging bit-manipulation subsequence

Posted on Wed, 20 May 2026 20:28:03 +0000 by wata