卡码网习题练习

#include <iostream>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;

int main(){
    int n; cin >> n;
    
    unordered_map<int, int> umap;
    
    for(int i = 0; i < n; i++){
        int tmp; cin >> tmp;
        umap[tmp] = i;
    }
    
    int a, b; cin >> a >> b;
    
    auto ita = umap.find(a);
    auto itb = umap.find(b);
    
    if(abs(ita->second - itb->second) == 1) cout<<"Yes"<<endl;
    else cout << "No"<<endl;
    
}

 

#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
#include <algorithm>

using namespace std;

int main(){
    
    int n, m; cin >> n >> m;
    
    long sum = 0;
    long res = INT_MAX;
    vector<vector<int>> pre_sum(n + 1, vector<int>(m + 1, 0));
    
    for(int i = 0; i < n; i++){
        for(int j = 0;  j< m; j++){
            int tmp; cin >> tmp; sum += tmp;
            pre_sum[i+1][j+1] = pre_sum[i][j+1] + pre_sum[i+1][j] + tmp - pre_sum[i][j];
        }
    }
    
    for(int i = 0; i < n + 1; i++){
        res = min(res, abs(sum - 2*pre_sum[i][m]));
    }
    
    for(int i = 0; i < m + 1; i++){
        res = min(res, abs(sum - 2*pre_sum[n][i]));
    }
    
    cout << res <<endl;
    
    return 0;
}

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

using namespace std;

int main(){
    
    int n; cin >> n;
    long long sum = 0;
    
    vector<long long> vec(n, 0);
    
    for(int i = 0; i < n; i++){
        cin >> vec[i];
        sum += vec[i];
    }
    
    int a, b; cin >> a >> b;
    
    int left  = min(a, b);
    int right = max(a, b);
    
    long long cur_sum = 0;
    for(int i = left; i < right; i++){
        cur_sum += vec[i - 1];
    }
    
    long long res = min(cur_sum, sum - cur_sum);
    
    cout << res <<endl;
    
    return 0;
}

#include <iostream>
#include <sstream>
#include <map>
#include <algorithm>
#include <vector>

using namespace std;

bool compareByValue(vector<int>& a, vector<int>& b) {
    return (a[1]-a[0]) < (b[1] - b[0]);
}

int main(){
    
    string data; cin >> data;
    
    stringstream ss(data);
    string str;
    
    vector<vector<int>>vec;
    
    while(getline(ss, str, ',')){
        string key, val;
        stringstream keyval(str);
        
        getline(keyval, key, ':');
        getline(keyval, val);
        vec.push_back({stoi(key), stoi(val)});
    }
    
    sort(vec.begin(), vec.end(), compareByValue);
    
    int res = vec[0][1];
    for(int i = 1; i < vec.size(); i++){
        res += vec[i][0];
        res = max(res, vec[i][1]);
    }
    if(res > 4800) cout<<-1<<endl;
    else cout<<res<<endl;
    
    
    return 0;
    
}

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

int search(vector<vector<int>>& vec, int obj){
    
    int left = 0, right = vec.size() - 1;
    
    while(left <= right){
        int mid = (left + right) / 2;
        if(vec[mid][0] >= obj) right = mid - 1;
        else if (vec[mid][0] < obj) left = mid + 1;
    }
    return left;
}

int main(){
    
    int freq1; cin >> freq1; getchar();
    
    string freq2; getline(cin, freq2);
    
    string str;
    stringstream ss(freq2);
    
    vector<vector<int>> vec;
    while(getline(ss, str, ' ')){
        string fre, loss;
        stringstream fre_loss(str);
        getline(fre_loss, fre, ':');
        getline(fre_loss, loss, ' ');
        
        vec.push_back({stoi(fre), stoi(loss)});
    }
    
    int left = search(vec, freq1);
    // cout <<left<<endl;
    
    double res;
    if(left < 0) res = vec[left + 1][1];
    else if(left == 0) res = vec[left][1];
    else if(left > vec.size() - 1) res = vec[left - 1][1];
    else if((freq1 - vec[left - 1][0]) > (vec[left][0] - freq1)) res = vec[left][1];
    else if((freq1 - vec[left - 1][0]) < (vec[left][0] - freq1)) res = vec[left - 1][1];
    else res = (vec[left][1] + vec[left - 1][1])/2.0;
    printf("%.1f",res);
    return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main(){
    
    int t; cin>>t;
    int flag1 = 0, flag2 = 0;
    
    vector<vector<char>> matrix(3, vector<char>(3));
    
    while(t--){
        for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++) 
            cin >> matrix[i][j];
        
        for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++){
            if(i == 1){
                if(matrix[i][j] == '*' && matrix[i-1][j] =='o' && matrix[i+1][j] =='o') flag2++;
                if(matrix[i][j] == 'o' && matrix[i-1][j] =='*' && matrix[i+1][j] =='*') flag1++;
            }
            if(j == 1){
                if(matrix[i][j] == '*' && matrix[i][j-1] =='o' && matrix[i][j+1] =='o') flag2++;
                if(matrix[i][j] == 'o' && matrix[i][j-1] =='*' && matrix[i][j+1] =='*') flag1++;
            }
        }
        
        if(flag1 == 0 && flag2 == 0) cout<<"draw"<<endl;
        else if(flag1 != 0 && flag2 != 0) cout<<"draw"<<endl;
        else if(flag1 != 0 && flag2 == 0) cout<<"kou"<<endl;
        else if(flag1 == 0 && flag2 != 0) cout<<"yukan"<<endl;
        flag1 = 0, flag2 = 0;
    }
        
        return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main(){
    
    int n; cin >> n;
    
    string cur_bin;
    cin >> cur_bin;
    
    int m; cin >> m;
    vector<string> yao1(m);
    vector<string> yao2(m);
    
    for(int i = 0; i < m; i++){
        cin >> yao1[i];
        cin >> yao2[i];
    }
    int q; cin>>q;
    
    for(int i = 0; i < q; i++){
        int cnt = 0;
        int tmp; cin >> tmp;
        
        for(int j = 0; j < n; j++){
            if(yao1[tmp - 1][j] == '1') cur_bin[j] = '0';
        }
        
        for(int j = 0; j < n; j++){
            if(yao2[tmp - 1][j] == '1') cur_bin[j] = '1';
        }
        
        for(int j = 0; j < n; j++){
            if(cur_bin[j] == '1') cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
    
}

#include <iostream>
#include <vector>

using namespace std;

int main(){
    int n ; cin >> n;
    
    vector<int> a(n, 0);
    vector<int> b(n, 0);
    
    for(int i = 0; i < n; i++) cin>>a[i];
    for(int i = 0; i < n; i++) cin>>b[i];
    
    vector<int> c(n, 0);
    for(int i = 0; i < n; i++) c[i] = a[i]-b[i];
    
    long tmp1 = 0, tmp2 = 0;
    for(int i = 0; i < n; i++){
        if(c[i] >= 0) tmp1 += c[i];
        else tmp2 += -c[i];
    }
    
    long res = tmp1 > tmp2 ? tmp1 : tmp2;
    
    cout<< res <<endl;
    
    return 0;
}

#include <iostream>
#include <vector>

using namespace std;


int main(){
    
    int n, m; cin >> n >> m;
    vector<int> a(n, 0);
    vector<int> b(n, 0);
    int res = -1, flag = 0;
    
    for(int i = 0; i < n; i++) cin >> a[i];
    
    for(int i = 0; i < m; i++){
        int l, r;
        cin >> l >>r;
        for(int j = l; j <=r; j++){
            b[j - 1]++;
            if(b[j - 1] > a[j - 1]) {flag = 1; break;}
        }
        if(flag == 1) {res = i; break;}
    }
    
    if(flag == 0) res = m;
    
    cout<<res<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值