2025“钉耙编程”中国大学生算法设计春季联赛(2)

1002 学历史导致的

枚举+模拟

#include <bits/stdc++.h>

using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;

string str1[] = {"jia", "yi", "bing", "ding", "wu", "ji", "geng", "xin", "ren", "gui"};
string str2[] = {"zi", "chou", "yin", "mao", "chen", "si", "wu", "wei", "shen", "you", "xu", "hai"};

void solve(){
    string str;
    cin >> str;
    int ans = 1984;
    int i = 0, j = 0;
    while(ans <= 2043){
        string res = str1[i] + str2[j];
        if(res == str) {
            cout << ans << endl;
            return;
        }
        i++, j++, ans++;
        if(i > 9) i = 0;
        if(j > 11) j = 0;
    }
    cout << -1 << endl;
}

int main(){
   ios::sync_with_stdio(0);
   cin.tie(0), cout.tie(0);
   int t = 1;
   cin >> t;
   while(t--){
       solve();
   }
   return 0;
}

1003 学数数导致的

枚举+计数

#include <bits/stdc++.h>

using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;
int inf = 0x3f3f3f3f;
const int N = 1e6+10;
int n;
int fir[N], cnt[N], vis[N];

void solve(){
    memset(fir, -1, sizeof(fir));
    memset(cnt, 0, sizeof(cnt));
    memset(vis, 0, sizeof(vis));
    cin >> n;
    vector<int> a(n+1);
    for(int i = 1; i <= n; i ++ ){
        cin >> a[i];
        if(fir[a[i]] == -1) fir[a[i]] = i;
    }
    for(int i = n-1; i >= 1; i -- ){
        if(!vis[a[i+1]] && a[i+1]){
            vis[a[i+1]] = 1;
            cnt[i] = cnt[i+1] + 1;
        } else {
            cnt[i] = cnt[i+1];
        }
    }
    ll ans = 0;
    int last = -1;
    for(int i = 1; i <= n; i ++ ){
        int x = a[i];
        int loc = fir[x];
        if(x == 0) last = i;
        else {
            if(loc != -1 && loc < last){
                ans += cnt[i];
                fir[x] = inf;
            }
        }
    }
    cout << ans << endl;
}

signed main(){
   ios::sync_with_stdio(0);
   cin.tie(0), cout.tie(0);
   int t = 1;
   cin >> t;
   while(t--){
       solve();
   }
   return 0;
}

1004 学DP导致的

经典一维DP--最长上升子序列

#include <bits/stdc++.h>

using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;

int get_k(string k_){
    int k = 0;
    reverse(k_.begin(), k_.end());
    for(int i = 0; i < k_.size(); i ++ ){
        k += (k_[i] - '0') * (int)pow(10, i);
        if(k >= 26) return 26;
    }
    return k;
}

void solve(){
    string str, k;
    cin >> str >> k;
    set<char> all;
    for(int i = 0; i < str.size(); i ++ ){
        all.insert(str[i]);
    }
    uint t =  min(26, get_k(k));
    if(t >= all.size()) {cout << all.size() << endl; return;}
    string temp = str;
    for(int i = 1; i < t; i ++ ) temp += str;
    int b[28] = {0};
    int ans = 1; b[1] = (int)temp[0]; 
    for(int i = 1; i < temp.size(); i ++ ){
        if((int)temp[i] > b[ans]){
            b[++ans] = (int)temp[i];
        } else {
            int l = 1, r = ans;
            while(l < r){
                int m = l + r >> 1;
                if((int)temp[i] <= b[m]) r = m;
                else l = m+1;
            }
            b[l] = (int)temp[i];
        }
    }
    cout << ans << endl;
}

signed main(){
   ios::sync_with_stdio(0);
   cin.tie(0), cout.tie(0);
   int t = 1;
   cin >> t;
   while(t--){
       solve();
   }
   return 0;
}

1005 学几何导致的

数学推公式

#include <bits/stdc++.h>

using namespace std;

#define int long long
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;

ll gets(ll x){
    return 1ll * x/2 * ((x+1)/2); 
}

void solve(){
    int n, k;
    cin >> n >> k;
    if(k % 2) {cout << 0 << endl; return;} 
    ll ans = 0;
    int cnt = k/2; 
    int sum = n/cnt; 
    int the_full = n - sum*cnt; 
    cnt -= the_full;
    ans = 1ll * gets(sum) * cnt + 1ll * gets(sum+1) * the_full;
    cout << ans << endl; 
}

signed main(){
   ios::sync_with_stdio(0);
   cin.tie(0), cout.tie(0);
   int t = 1;
   cin >> t;
   while(t--){
       solve();
   }
   return 0;
}

1006 学博弈论导致的

简单博弈论

#include <bits/stdc++.h>

using namespace std;

#define int long long
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;

void solve(){
    int a, b, c;
    cin >> a >> b >> c;
    ll sum = (a * 1LL)%4 + (b * 2LL)%4 + (c * 4LL)%4;
    if(sum % 4) puts("Alice");
    else puts("Bob");
}

signed main(){
   ios::sync_with_stdio(0);
   cin.tie(0), cout.tie(0);
   int t = 1;
   cin >> t;
   while(t--){
       solve();
   }
   return 0;
}
posted @ 2025-03-24 22:57  算法蒟蒻沐小白  阅读(38)  评论(0)    收藏  举报