2.2 总体复盘

· · 个人记录

题目

T1

很简单的题,但是要注意第一层循环的结束条件是没有的,因为 可能n \le i。

卡分点:n \le i

T2

使用前缀和的思想。

先用 sum_i 来计算和,如果发现在加上这个数之前,并没有大于 s,那么就加上这个数。

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1ll
#define rc(x) x << 1ll | 1ll 
#define lowbit(x) ((x) & (-x)) 
#define int long long 
const int N = 1e5 + 7; 
int a[N], sum[N]; 
void solve() {
    int n, s;
    std::cin >> n >> s; 
    rep(i, 1, n) {
        std::cin >> a[i]; 
        sum[i] = sum[i - 1] + a[i]; 
    }
    int max = 0, ans = 0, m = 0; 
    rep(i, 1, n) {
         if(max < a[i]) {
            max = a[i]; m = i; 
         }
         if(sum[i] - max <= s) {
            ans = m; 
         }
    }
    if(sum[n] <= s) std::cout << 0 << std::endl; 
    else std::cout << ans << std::endl; 
}

signed main() {
    IOS; 
    int t; std::cin >> t; 
    while(t--)solve(); 
    return 0; 
}

卡分点:十年OI一场空,不开LL见祖宗。

T3

对于序列 a,我们可以枚举所谓的 k,可以发现的是,k 的左边部分是单调下降的,右边部分是单调上升的,让序列中的每个元素最大,这样总和的答案就最大了。

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1ll
#define rc(x) x << 1ll | 1ll 
#define lowbit(x) ((x) & (-x))
#define int long long
const int N = 1e5 + 7; 
const int inf = 0x3f3f3f3f;
int a[N], b[N]; 
int ans, id; 
int min, tmp; 
void solve() {
    int n; 
    std::cin >> n; 
    rep(i, 1, n) std::cin >> a[i]; 
    rep(i, 1, n) {
        min = a[i], tmp = a[i]; 
        for(int j = i - 1; j >= 1; j--) {
            min = std::min(min, a[j]); 
            tmp += min; 
        }
        min = a[i]; 
        for(int j = i + 1; j <= n; j++) {
            min = std::min(min, a[j]); 
            tmp += min; 
        }

        if(tmp > ans) {
            ans = tmp; 
            id = i;
        }
    }
    for(int i = id - 1; i >= 1; i--) {
        a[i] = std::min(a[i], a[i + 1]); 
    }

    for(int i = id + 1; i <= n; i++) {
        a[i] = std::min(a[i], a[i - 1]); 
    }

    rep(i, 1, n) {
        std::cout << a[i] << " " ;
    }
}

signed main() {
    IOS; 
    solve(); 
    return 0; 
}

卡分点:十年OI一场空,不开LL见祖宗 (*2)

T4

对于每种可能的解,(m + b_i - a_1) \% m,我们只要对 b 和 c 进行排序比较即可。

#include <bits/stdc++.h>
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--) 
#define lc(x) x << 1ll
#define rc(x) x << 1ll | 1ll
#define lowbit(x) ((x) & (-x))
#define int long long 

const int N = 2e3 + 7; 
const int inf = INT_MAX; 
int a[N], b[N], c[N]; 
void solve() {
    int n, m; 
    std::cin >> n >> m; 
    rep(i, 1, n) {
        std::cin >> a[i];  
    }
    rep(i, 1, n) {
        std::cin >> b[i]; 
    }
    std::sort(b + 1, b + 1 + n); 
    int min = inf; 
    rep(i, 1, n) {
        int cal = (m + b[i] - a[1]) % m; 
        bool flag = true; 
        rep(j, 1, n) {
            c[j] = (a[j] + cal) % m; 
        }
        std::sort(c + 1, c + 1 + n); 
        rep(j, 1, n) {
            if(c[j] % m != b[j] % m){
                flag = false; 
            }
        }
        if(flag) {
            min = std::min(min, cal); 
        }
    }
    std::cout << min << std::endl; 
}

signed main() {
     IOS; 
     solve(); 
     return 0; 
}

卡分点:看清楚 n 的大小,这题十分玄学,空间开不够显示的是 WA,而不是 RE。

T5

高精除以低精模板,我也不知道为什么会放进比赛里...

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1ll
#define rc(x) x << 1ll | 1ll 
#define lowbit(x) ((x) & (-x))
std::vector<int> div (std::vector<int> &a, int b, int &r){
    std::vector<int> c; 
    for(int i = 0; i < a.size(); i++) {
        r = r * 10 + a[i]; 
        c.push_back(r / b);
        r = r % b; 
    }
    reverse(c.begin(), c.end()); 
    while(c.size() > 1 && c.back() == 0) c.pop_back(); 
    return c; 
}
void solve() {
    std::string s; std::cin >> s; 
    std::vector<int> v; 
    for(int i = 0; i < s.size(); i++) {
        v.push_back(s[i] - '0'); 
    }
    int r = 0; 
    auto ans = div(v, 13, r); 
    for(int i = ans.size() - 1; i >= 0; i--) {
        std::cout << ans[i]; 
    } 
    std::cout << std::endl; 
    std::cout << r << std::endl; 
}

int main() {
    IOS; 
    solve(); 
    return 0; 
}

卡分点:无。

T6

方法一

枚举年、月、日。根据年月日的进位规则来写,但是这样写的代码量较大,而且时间复杂度还比较高。

方法二

实际上所谓回文年月日中的年取决于日和月,所以只要枚举月和日即可,并且用 tmp 来计算:`int temp = (j % 10 1000 + j / 10 100 + i % 10 * 10 + i / 10)

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--) 
#define lc(x) x << 1 
#define rc(x) x << 1 | 1
#define lowbit(x) ((x) & (-x))  
#define int long long

const int N = 1e5 + 7; 
const int month[20] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
int cnt; 
void solve() {
    int n, m; 
    std::cin >> n >> m; 
    rep(i, 1, 12) {
        rep(j, 1, month[i]){
            int temp = (j % 10 * 1000 + j / 10 * 100 + i % 10 * 10 + i / 10) 
                        * 10000 + i * 100 + j; 
            if(temp < n || temp > m) continue; 
            else cnt++; 

        }
    }
    std::cout << cnt << std::endl; 
}
signed main() {
    IOS; 
    solve(); 
    return 0; 
}

卡分点:无。

T7

方法一

同T6,枚举年月日并计算即可。

方法二

其实这是一道很好的求星期几的天数的模板题,利用求星期几公式来计算星期几并计数即可。

公式:int Week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--) 
#define lc(x) x << 1 
#define rc(x) x << 1 | 1
#define lowbit(x) ((x) & (-x))  
#define int long long

const int N = 1e5 + 7; 

int a[N]; 
int cal(int y, int m, int d)  
{  
    if(m == 1 || m == 2)  
    {  
        m += 12;  
        y--;  
    }  
    int Week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;  
    switch(Week)  
    {  
        case 0: return 1; break;   
        case 1: return 2; break;  
        case 2: return 3; break;  
        case 3: return 4; break;  
        case 4: return 5; break;  
        case 5: return 6; break;  
        case 6: return 7; break;  //周日 
    }  
} 
int cnt1, cnt2, cnt3, cnt4, cnt5, cnt6, cnt7; 
void solve() {
    int n; 
    std::cin >> n; 
    int l = 1900, r = 1900 + n - 1; 
    rep(i, l, r) {
        rep(j, 1, 12) {
            if(cal(i, j, 13) == 6) {
                cnt1++; 
            } 
            if(cal(i, j, 13) == 7) {
                cnt2++; 
            }
            if(cal(i, j, 13) == 1) {
                cnt3++; 
            }
            if(cal(i, j, 13) == 2) {
                cnt4++; 
            }
            if(cal(i, j, 13) == 3) {
                cnt5++; 
            }
            if(cal(i, j, 13) == 4) {
                cnt6++; 
            }
            if(cal(i, j, 13) == 5) {
                cnt7++; 
            }
        }
    }
    std::cout << cnt1 << " " << cnt2 << " " << cnt3 << " " << cnt4 << " " << cnt5 << " " << cnt6 << " " << cnt7 << std::endl; 

}

signed main() {
    IOS; 
    solve(); 
    return 0; 
}

卡分点:无。