1804D - Accommodation - CodeForces Solution


dp greedy

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <cassert>
#include <math.h>
 
using namespace std;
 
using pii = pair<int,int>;
using vi = vector<int>;

#define endl "\n"
#define int int64_t 
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define FOR0(i,a) FOR(i,0,a)
#define FOR1(i,a) for (int i = (1); i <= (a); ++i)
#define TRAV(a,x) for (auto& a: x)



int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,M;
	cin>>N>>M;
	int minAns=0;
	int maxAns=0;
	FOR0(i,N){
		string a;
		cin>>a;
		int ans=0,ans2=0;
		TRAV(i,a){
			if(i=='1'){
				ans++;
				ans2++;
			}
		}
		//int ans=0;
		bool vis[M];
		FOR0(i,M){
			vis[i]=false;
		}
		int cnt2=0;
		for(int i=0;i<M-1;i++){
			if(cnt2>=M/4){continue;}
			if(a.at(i)=='1' && a.at(i+1)=='1'){
				vis[i]=true;
				vis[i+1]=true;
				i++;
				cnt2++;
				ans--;
			}
		}
		

		//int ans2=0;
		//bool vis2[M];
		FOR0(i,M){
			vis[i]=false;
		}
		int cnt=0;
		for(int i=0;i<M-1;i++){
			if(cnt>=M/4){continue;}
			if(!(a.at(i)=='1' && a.at(i+1)=='1')){
			//	cout<<"    "<<i<<" "<<i+1<<endl;
				vis[i]=true;
				vis[i+1]=true;
				i++;
				cnt++;
				//ans2++;
			}
		}
		for(int i=0;i<M-1;i++){
			if(cnt>=M/4){continue;}
			if(vis[i]==false && vis[i+1]==false){
			//	cout<<"    "<<i<<" "<<i+1<<endl;
				vis[i]=true;
				vis[i+1]=true;
				i++;
				cnt++;
				ans2--;
			}
		}
		minAns+=ans;
		maxAns+=ans2;

	}
	cout<<minAns<<" "<<maxAns<<endl;


	return 0;
}


Comments

Submit
0 Comments
More Questions

1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant