946D - Timetable - CodeForces Solution


dp *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <chrono>
#define ll long long
#define ld long double
#define fe first
#define se second
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int maxn=100000+2;
ll mod=998244353;


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n,m,l;
    cin>>n>>m>>l;
    string s[n];
    for(int i=0;i<n;i++)cin>>s[i];

    int dp[n][m+1];
    for(int i=0;i<n;i++)for(int j=0;j<=m;j++)dp[i][j]=1e7;

    for(int i=0;i<n;i++){
        int cnt1 = 0;
        for(int j=0;j<m;j++){
            if(s[i][j] == '0')continue;
            int cnt2 = 0;
            for(int k = m-1;k>j;k--){
                if(s[i][k] == '0')continue;
                dp[i][cnt1 + cnt2] = min(dp[i][cnt1+cnt2],(k-j+1));
                cnt2++;
            }
            cnt1++;
        }
        dp[i][cnt1] = 0;
        if(cnt1 > 0)dp[i][cnt1 - 1] = 1;
    }

    vector<int>ve(l+1,-1);
    ve[0] = 0;

    for(int i=0;i<n;i++){
        vector<int>temp(l+1,-1);
        for(int j=l;j>=0;j--){
            for(int k=0;k<=m;k++){
                if(dp[i][k] != 1e7 && j-k >= 0)temp[j] = (temp[j] == -1 ? ve[j-k] + dp[i][k] : min(temp[j],ve[j-k] + dp[i][k]));
            }
        }
        swap(ve,temp);
    }
    
    int ans = 1e7;
    for(int i=0;i<=l;i++)if(ve[i] != -1)ans = min(ans,ve[i]);
    cout<<ans;
}


Comments

Submit
0 Comments
More Questions

1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left