D. Accommodation

Problem - 1804D - Codeforces

 

思路:首先我们可以统计一下开着灯的个数,对于一个卧室的来说, 不会存在让灯减少的情况,而对于两个卧室的来说,存在4种情况,00,01,10,11,只有在11的情况下,灯的数量会减少一个,因为这两个灯在同一个房子内,只算一遍,所以灯的数量会减少。那么我们就得到了一种情况,就是只有在11的情况下灯的数量会减少。那么对于最小值的情况,我肯定是希望11的数量越多越好,所以我只需要统计连续的1的长度,length,那么能够减少的数量就是length/2,统计完每一段连续的1之后,得到了sum,那么最终减少的数量就是min(sum,str.size()/4)。而对于最大值的情况,那么我们就希望11的数量越少越好,所以两个卧室的更多的用来匹配00,01,10,所以我们可以先遍历一遍数组,然后将所有的00,01,10先占用了两个卧室的,然后看看是否有剩余,如果有剩余的两个卧室的,则用11的占用了,剩下的没有被使用过的就是分配一个卧室的,直接统计出来即可又因为每行直接是没有关系的,所以我们直接每一行单独考虑,然后加在一起即可

// Problem: D. Accommodation
// Contest: Codeforces - Nebius Welcome Round (Div. 1 + Div. 2)
// URL: https://codeforces.com/problemset/problem/1804/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms

#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<bitset>
#include<deque>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<vector> 
#include<set>
#include<cstdlib>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int> > PIII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
char str[N];
bool st[N];

int get1() {
	int n=strlen(str+1);
	int res=0;
	for(int i=1;i<=n;i++) if(str[i]=='1') res++;
	int last=0;
	int sum=0;
	for(int i=1;i<=n;i++) {
		if(str[i]=='1') last++;
		else {
			sum+=last/2;
			last=0;
		}
	}
	sum+=last/2;
	res-=min(sum,m/4);
	
	return res;
}

int get2() {
	int n=strlen(str+1);
	int res=0;
	int a=n/4;
	memset(st,false,sizeof(bool)*(n+4));
	for(int i=1;i<=n;i++){
		if(i+1<=n) {
			if(str[i]=='0'&&str[i+1]=='0') {
				st[i]=st[i+1]=true;
				a--;
				i++;
			}else if(str[i]=='1'&&str[i+1]=='0') {
				st[i]=st[i+1]=true;
				res++;
				a--;
				i++;
			}else if(str[i]=='0'&&str[i+1]=='1') {
				st[i]=st[i+1]=true;
				res++;
				a--;
				i++;
			}
		}
		if(a==0) break;
	}
	
	if(a!=0) {
		for(int i=1;i<=n;i++) {
			if(st[i]) continue;
			if(i+1<=n) {
				if(st[i+1]) continue;
				if(str[i]=='1'&&str[i+1]=='1') {
					st[i]=st[i+1]=true;
					a--;
					res++;
				}
			}
			if(a==0) break;
		}
	}
	
	for(int i=1;i<=n;i++) {
		if(st[i]) continue;
		if(str[i]=='1') res++;
	}
	
	return res;
}

void solve() {
	n=read(),m=read();
	
	int ansl=0,ansr=0;
	for(int i=1;i<=n;i++) {
		scanf("%s",str+1);
		int l=get1();
		int r=get2();
		ansl+=l,ansr+=r;
		// printf("%d %d\n",l,r);
	}
	
	printf("%d %d\n",ansl,ansr);
}   

int main() {
    // init();
    // stin();

    // scanf("%d",&T);
    T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值