暑假算法训练day10(Educational Codeforces Round 131 +Acwing周赛)

42 篇文章 2 订阅
20 篇文章 0 订阅

A. Grass Field

给一个2*2的格子,分别填上0 和 1 ,每次选择一行and一列,要求让格子变成0的最小步数
分类讨论即可

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
void solve()
{
	int cnt=0;
	for(int i=1;i<=2;i++)
	{
		for(int j=1;j<=2;j++)
		{
			int x;
			cin>>x;
			if(x==1)cnt++;
		}
	}
	if(cnt>=1&&cnt<=3)cout<<1<<endl;
	else if(cnt==0)cout<<0<<endl;
	else cout<<2<<endl;
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

B. Permutation

给一个长度为n的排列, 1 < = i < n 1<=i<n 1<=i<n使 p i ∗ d = p i + 1 p_i*d=p _{i+1} pid=pi+1,例如,如果d=3,p=[5,2,6,7,1,3,4],那么这种排列组合的代价是2,因为 p 2 ⋅ 3 = p 3 , p 5 ⋅ 3 = p 6 p_2⋅3=p_3,p_5⋅3=p_6 p23=p3p53=p6
求这样的对数最多的cost
不难发现cost为2的时候对数可能最多。
直接模拟即可

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=2e5+10;
bool st[N];
void solve()
{
	cin>>n;
	cout<<2<<endl;
	for(int i=1;i<=n;i++)st[i]=false;
	for(int i=1;i<=n;i++)
	{
		for(int j=i;j<=n;j*=2)
		{
			if(st[j])continue;
			cout<<j<<' ';
			st[j]=true;
		}
	}
	for(int i=1;i<=n;i++)
		if(!st[i])cout<<i<<' ';
	cout<<endl;
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

C. Schedule Management

n个工人,m个任务,给定一个长度为m的数组, a i a_i ai表示的是 a i a_i ai i i i精通(只用花一个小时),否则2个小时,求完成工作的最少时间
想到二分解决这个问题,check函数赛时调了很久。。。
check实现当时间为x最大任务完成量>=m即可

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=2e5+10;
int a[N];
map<int,int>mp;
bool check(int x)
{
	int res=0;
	for(int i=1;i<=n;i++)
	{
		res+=min(mp[i],x);
		res+=max(0ll,x-mp[i])/2;
	}
	return res>=m;
}
void solve()
{
	cin>>n>>m;
	mp.clear();
	for(int i=1;i<=m;i++)cin>>a[i],mp[a[i]]++;
	int l=1,r=1e9;
	while(l<r)
	{
		int mid=l+r>>1;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	cout<<l<<endl;
	
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

D. Permutation Restoration

题意不在赘述。
根据 b i b_i bi i i i可以算出来 a i a_i ai的取值范围,

  1. b i = 0 b_i=0 bi=0时, i + 1 < = a i < = n i+1<=a_i<=n i+1<=ai<=n(不预处理会tle)
  2. b i ≠ 0 时 b_i≠0时 bi=0 i / ( b i + 1 ) < = a i < = i / b i i/(b_i+1)<=a_i<=i/b_i i/(bi+1)<=ai<=i/bi
    在这里插入图片描述
    随后就是一个贪心的思路,右端点从小到大排,其次再按左端点从小到大排。
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=1e6+10;
int b[N];
struct node
{
	int l,r,idx;
};
bool cmp(node a,node b)
{
	if(a.r!=b.r)return a.r<b.r;
	return a.l<b.l;
}
void solve()
{
	vector<node>ans;
	cin>>n;
	vector<int>v(n+10),res(n+10);
	for(int i=1;i<=n;i++)
	{
		cin>>b[i];
		int l,r;
		if(b[i]==0)
		{
			l=i+1,r=n;
		}
		else if(b[i]==i)
		{
			v[1]=1;
			res[i]=1;
			l=i/(b[i]+1)+1,r=i/b[i];
		}
		else l=i/(b[i]+1)+1,r=i/b[i];
		if(l==r)
		{
			res[i]=l;
			v[l]=1;
		}
		else ans.pb({l,r,i});
	}
	sort(all(ans),cmp);
	int pos=1;
	for(auto x:ans)
	{
	    while(v[pos])pos++;
		int l=x.l,r=x.r,idx=x.idx;
		for(int i=max(pos,l);i<=r;i++)
		{
			if(v[i]==0)
			{
				v[i]=1;
				res[idx]=i;
				break;
			}
		}
	}
	for(int i=1;i<=n;i++)cout<<res[i]<<' ';
	cout<<endl;
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

E. Text Editor

首先这题不能define int long long 会MLE
具体看代码的注释

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
//#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=5010;
int dp1[N][N];
int dp2[N][N];
int s[N];//b的第i个与a_j匹配 s[i]=j;
void solve()
{
	cin>>n>>m;
	string a,b;
	cin>>a>>b;
	a=" "+a;
	b=" "+b;
	for(int i=1,j=1;i<=m;i++,j++)
	{
		while(j<=n&&a[j]!=b[i])j++;
		s[i]=j;
	}
	s[m+1]=n+1;
	if(s[m]>n)//最后一位不匹配
	{
		cout<<-1<<endl;
		return;
	}
	for(int i=0;i<=n+1;i++)
		for(int j=0;j<=m+1;j++)
			dp1[i][j]=dp2[i][j]=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i]==b[j])
				dp1[i][j]=dp1[i-1][j-1]+1;前缀子段每一个长度为i的字符串变成长度为j的匹配字符串不需要移动的花费。
	for(int i=n;i;i--)
		for(int j=m;j;j--)
			if(a[i]==b[j])
				dp2[i][j]=dp2[i+1][j+1]+1;后缀每一个长度为i的字符串变成长度为j的匹配字符串不需要移动的花费。
	int res=2*n;
	for(int i=0;i<=m;i++)
	{
		for(int j=s[i];j<=s[i+1]-1;j++)
		{
			int cost1=j-i+j-dp1[j][i];//j-i删除字符 
			int cost2=n-j-dp2[j+1][i+1];
			res=min(res,cost1+cost2+(cost1>0)); 
		}
	}
	cout<<res<<endl;
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

4491. 数组操作

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
//#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=110;
int a[N];
int s[N];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
	int minv=ll_INF;
	for(int i=0;i<=n;i++)minv=min(minv,s[i]);
	cout<<s[n]-minv<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

4492. 减法操作

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
bool cmp(int n)
{
	for(int i=2;i<=n/i;i++)
		if(n%i==0)return false;
	return true;
}
void solve()
{
	cin>>n;
	int x=n;
	for(int i=2;i<=n/i;i++)
	{
		if(n%i==0)
		{
			x=i;
			break;
		}
	}
	if(!cmp(n))cout<<1+(n-x)/2<<endl;
	else cout<<1<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

4493. 环形连通分量

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
//#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=2e5+10;
int f[N];
int d[N],cnt[N];
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int a,int b)
{
	int x=find(a),y=find(b);
	if(x!=y)
	{
		cnt[x]+=cnt[y];
		f[y]=x;
	}
}
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)f[i]=i,cnt[i]=1;
	for(int i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		d[a]++,d[b]++;
		unite(a,b);
	}
	vector<vector<int>>v(n+1);
	for(int i=1;i<=n;i++)
	{
		if(cnt[find(i)]==1)continue;
		v[find(i)].pb(i);
	}
	int cnt=0;
	for(int i=0;i<v.size();i++)
	{
		if(v[i].size()>1)
		{
			bool f=false;
			for(int j=0;j<v[i].size();j++)
			{
				if(d[v[i][j]]!=2)f=true;
			}
			if(!f)cnt++;
		}
	}
	cout<<cnt<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值