第四次训练课总结

· · 题解

T1:

解题思路:

1、通过枚举所有的子矩阵,找出所有子矩阵中满足条件的最小矩阵。

2、枚举左上角以及右下角来找出所有的矩阵。

代码:

#include<bits/stdc++.h>
using namespace std;
int a[10010][10010];
int main(){
    int n , m , k;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= m; ++j){
            cin >> a[i][j];
        }
    }
    int ans = 1000000;
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= m; ++j){
            for (int x = i; x <= n; ++x){
                for (int y = j; y <= m; ++y){
                    int sum = 0; 
                    for (int p = i; p <= x; ++p){
                        for (int q = j; q <= y; ++q){
                            if(a[p][q] == 1){
                                sum ++;
                            }
                            if (sum >= k){
                                ans = min(ans , (x - i + 1) * (y - j + 1));
                            }
                        }
                    }
                }
            }
        }
    }
    cout << ans;
    return 0;
}

T2:

解题思路:根据题目要求来判断一下在每一行每一列输出什么图案(会覆盖之前的图案)。

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= n;j ++){
            if(j == 1 || j == n){
                cout << "|";
            }
            else if(i == 1 || i == n || i == n / 2 + 1){
                cout << "-";
            }
            else{
                cout << "x";
            }
        }
        if(i != n)
        cout << endl;
    }
    return 0;
} 

T3:

解题思路:

1、传送器可以使用一次。如果我在城市I的话我可以传送到i-1号城市或者i+1号城市。

2、如果不用传送器,那么我们所需要的时间 = a[1] + a[2] + a[3] + a[4] + ...... + a[n - 1]。

如果从i号城市开始传送,那么一定是传送到i + k ,所以从i ~ i + k是不需要消耗时间的。

代码:

#include<bits/stdc++.h>
using namespace std;
long long a[1000010] , sum[1000010] , maxn;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0) , cout.tie(0);
    int n , k;
    cin >> n >> k;
    for(int i = 2;i <= n;i ++){
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
        int ans = max(1 , i - k + 1);
        maxn = max(maxn , sum[i] - sum[ans - 1]);
    }
    cout << sum[n] - maxn;
    return 0;
} 

T4:

解题思路:现在让我们安排牛的到达顺序,使得最终发出去的柠檬汽水最少,也就是我们要使得最终在排队的奶牛最少。显然我们让耐心更强的牛排在前面会更好。

所以直接从大到小排序即可。 然后从前往后遍历所有牛,判断这头牛是否排队

代码:

#include<bits/stdc++.h>
using namespace std;
int a[100010];
int cmp(int x , int y){
    return x > y;
}
int main(){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++){
        cin >> a[i];
    }
    int cnt = 0;
    sort(a + 1 , a + 1 + n , cmp);
    for(int i = 1;i <= n;i ++){
        if(cnt <= a[i]){
            cnt ++;
        }
    }   
    cout << cnt;
    return 0;
} 

T5:

解题思路:我们要求的没有被挡住的面积。

没有被挡住的面积 = 总面积 - 被挡住的面积。

——图已给老师。

代码:

#include<bits/stdc++.h>
using namespace std;
int a[2010] , b[2010] , c[2010] , d[2010];
int main(){
    for(int i = 1;i <= 3;i ++){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    int ans = 0;
    for(int i = 1;i <= 2;i ++){
        int sum = (c[i] - a[i]) * (d[i] - b[i]);
        int s = max(0 , (min(d[3] , d[i]) - max(b[3] , b[i])));
        int k = max(0 , (min(c[3] , c[i]) - max(a[3] , a[i])));
        ans += (sum - s * k);
    }
    cout << ans;
    return 0;
} 

T6:

题目描述:每头牛以s[i]的速度行驶,但是如果他前面有一头牛那么他的速度就需要减少D,那么如果有K头牛那么要减少K * D。 但是我们高速公路有最低限速是L,如果一头牛的速度低于了最低限速,那么他就不能上高速公路。 当然这个题目一共有M条高速公路,现在让我们求出最多有多少头牛可以在告诉公路上行驶。 只要一头最终他的速度满足最低限速他就可以在高速公路上行驶。

解题思路: 一头牛最终的速度 = s[i] - k D; 这是一头牛最终的最大速度,那么如果s[i] - k d >= L,那么 这头牛可以行驶在高速公路上。

我们要安排每一头的位置,来使得最多的牛能够在高速公路上行驶。

先假设我们只有一条高速公里,那么显然我们怎么安排牛的位置最好?

把速度慢的放前面最好,因为放在越后面他需要降速越多。 从小到大按照速度排序, 从前往后枚举一下,哪一些牛能够上高速。

代码:

#include<bits/stdc++.h>
using namespace std;
int s[50010] , a[50010] , l[50010];
int main(){
    int n ,m , x , y;
    cin >> n >> m >> x >> y;
    for(int i = 1;i <= n;i ++){
        cin >> s[i];
    }
    sort(s + 1 , s + 1 + n);
    int cnt = 0;
    for(int i = 1;i <= n;i ++){
        if(s[i] - l[i % m + 1] * x >= y){
            cnt ++;
            l[i % m + 1] ++;
        }   
    }
    cout << cnt;
    return 0;
} 

T7未讲解

代码已写:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string str;
    cin >> str;
    long long cnt = 0 , ans = 0;
    for(int i = 0;i < str.size();i ++){
        if(str[i] == 'z'){
            cnt ++;
        }
        if(str[i] == 'y'){
            ans += cnt * (cnt - 1) / 2;
        }
    }
    cout << ans;
    return 0;
}