548B - Mike and Fun - CodeForces Solution


brute force dp greedy implementation *1400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
 
// -------------------------------------- -------------------------------------- //

//#define int long long
#define mod 1000000007
#define f(a,b) for(int i=a;i<b;i++)
#define fr(a,b) for(int i=a;i>b;i--)
#define endl '\n'
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key(), *find_by_order()
vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
#define fi for(int i=0;i<n;i++)
#define fj for(int j=0;j<n;j++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

// -------------------------------------- -------------------------------------- //

int divisors(int n){

    set<int> v;
    for (int i=1; i<=sqrt(n); i++){
        if (n%i == 0)
        {
            if (n/i == i)
                v.insert(i);
  
            else{
                v.insert(i); v.insert(n / i);
            }
        }
    }

    return *(--(--v.end()));

}

int ask_judge(int l, int r){

    if(l >= r) return -1;
    cout<<"? "<<l + 1<<" "<<r + 1<<endl;

    int x; cin>>x;
    
    return x - 1;

}

int isprime(int n){

    for(int i=2;i*i<=n;i++){
        if(n % i == 0) return 0;
    }
    return 1;

}

bool check_palindrome(string a){
    string b = a;
    reverse(a.begin(), a.end());
    return a == b;
}
//vector<bool> vis;

vector<int> parent;
vector<int> rankk;

int findPar(int node){
    if(node == parent[node]){ return node; }
    return parent[node] = findPar(parent[node]);
}

void union_it(int v, int u){
    v = findPar(v); u = findPar(u);

    if(rankk[v] == rankk[u]){
        parent[v] = u;
        rankk[u]++;
    }
    else if(rankk[v] > rankk[u]){
        parent[u] = v;
    }
    else parent[v] = u;

}

vector<int> vis, viss;

int curr;
int cnt;

void dfs(int node, map<int, int>& adj, int start, vector<vector<int>>& tohan){

    vis[node] = 1;
    auto it = adj[node];
    if(!vis[it]){
        tohan[start].push_back(it);
        dfs(it, adj, start, tohan);
    }
    else{
        bool f = 0;
        for(auto i: tohan[start]){
            if(i == it){
                f = 1;
                break;
            }
        }
        if(!f) tohan[start].push_back(it);
        return;
    }
    
}

int helper(int i, int sum, int v2, int t, int d, vector<vector<int>>& dp){

    if(i == t){
        if(sum == v2) return 0;
        return INT_MIN;
    }

    if(dp[i][sum] != -1) return dp[i][sum];

    int op = INT_MIN;
    for(int j=(-d);j<=d;j++){
        int x = helper(i + 1, sum + j, v2, t, d, dp);
        if(x != INT_MIN) op = max(op, (sum + j) + x);
    }

    return dp[i][sum] = op;

}

int32_t main()
{

    IOS;

    int test = 1; //cin>>test;

    while(test--){
    
        //int n; cin>>n;
        //int k; cin>>k;
        //vector<int> v(n); fi cin>>v[i];
        
        int n, m; cin>>n>>m;
        int q; cin>>q;

        vector<vector<int>> v(n, vector<int>(m));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>v[i][j];
            }
        }

        for(int k=0;k<q;k++){

            int ans = 0;
            int x, y; cin>>x>>y;
            v[x - 1][y - 1] = !v[x - 1][y - 1];
            for(int i=0;i<n;i++){
                int cnt = 0;
                for(int j=0;j<m;j++){
                    if(v[i][j] == 1) cnt++;
                    else cnt = 0;                    
                    ans = max(ans, cnt);
                }
                ans = max(ans, cnt);
            }
            cout<<ans<<endl;

        }

    }

    return 0;
                
}


Comments

Submit
0 Comments
More Questions

174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes