Codeforces Round 857 (Div. 2)

题目链接

A

核心思路

读懂题目也就不难了。

// Problem: A. Likes
// Contest: Codeforces - Codeforces Round 857 (Div. 2)
// URL: https://codeforces.com/contest/1802/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
#define endl "\n"
#define int long long 

void solve()
{
	int n;
	cin>>n;
	vector<int> a(n+1);
	int cnt_p=0,cnt_ip=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]>0)
		{
			cnt_p++;
		}
		else
		cnt_ip++;
	}
	for(int i=1;i<=cnt_p;i++)
	cout<<i<<" ";
	for(int i=1;i<=cnt_ip;i++)
	{
		cout<<cnt_p-i<<" ";
	}
	cout<<endl;
	for(int i=1;i<=cnt_ip;i++)
	{
		cout<<1<<" "<<0<<" ";
	}
	cnt_p-=cnt_ip;
	for(int i=1;i<=cnt_p;i++)
	cout<<i<<" ";
	cout<<endl;
}


signed main()
{
int t;
cin>>t;
while(t--)
{
	solve();
}
}

B

核心思路

这个的一个主要思想是首先如果我们遇到了2,也就是可以对雌雄分类。那么我们就可以在已经开辟的笼子里空出来几个笼子来给以后的兔子使用。

这里有一个很关键的地方,那就是需要统计兔子的总数,也就是需要划分奇数和偶数。如果是偶数那么就需要多占用一个笼子,比如:雌 雌 雌 雄。我们可以发现这个其实需要多占用一个笼子。

// Problem: B. Settlement of Guinea Pigs
// Contest: Codeforces - Codeforces Round 857 (Div. 2)
// URL: https://codeforces.com/contest/1802/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
#define endl "\n"
#define int long long 

void solve()
{
	int n;
	cin >> n;
	vector<int> a(n + 1);
	//	int cnt_1 = 0;
	int ans = 0;
	for (int i = 1;i <= n;i++)
		cin >> a[i];
	int remain = 0;
	int cnt_1 = 0;
	for (int i = 1;i <= n;i++)
	{
		if (a[i] == 1)
		{
			if(remain>0)
			remain--;
			else
			ans++;
			cnt_1++;
		}
		else
		{
		//	cout<<cnt_1<<" "<<1<<endl;
			int t = (cnt_1 +1) / 2;
			remain = ans - t;
			if(cnt_1%2==0)
			remain--;
			//cout<<endl;
		//	cout << remain <<  " " << 0 << endl;
			//cout<<endl;
			//ans += cnt_1;
		//	cnt_1 = 0;
		}
	}
	// if (cnt_1)
	// {
		// //cout<<cnt_1<<endl;
		// ans += cnt_1;
	// }
	cout << ans << endl;
}


signed main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
}

C

核心思路

这里学到一个种很新颖的做法。

就是先随机化我们的第一行和第一列。然后顺便把每个2*2的矩阵的异或值也随机化规定下。

接下来就是一行一行的递推就好了。

// Problem: C. The Very Beautiful Blanket
// Contest: Codeforces - Codeforces Round 857 (Div. 2)
// URL: https://codeforces.com/contest/1802/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
#define endl "\n"
#define int long long 

 mt19937_64 rnd(random_device{}());
uniform_int_distribution<LL> dist(0, LLONG_MAX);

void solve()
{
	int n,m;
	cin>>n>>m;
	 vector<vector<LL> > a(n, vector<LL>(m));
	while(1)
	{
		for(int i=0;i<n;i++)
	a[i][0]=dist(rnd);
	
	set<int> s;
	for(int i=0;i<m;i++)
	a[0][i]=dist(rnd);
	int val=dist(rnd);
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<m;j++)
		a[i][j]=val^a[i-1][j]^a[i][j-1]^a[i-1][j-1];
	}
	for(int i=0;i<n;i++)
	for(int j=-0;j<m;j++)
	s.insert(a[i][j]);
	if(s.size()==n*m)
	{
		cout<<n*m<<endl;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			cout<<a[i][j]<<" ";
			cout<<endl;
		}
		break;
	}
	}
	
}

signed main()
{
int t;
cin>>t;
while(t--)
{
	solve();
}
}

D

核心思路

这个题目首先得理清楚题目的意思,题目是要我们其中一个朋友只可以在第一家商店买东西,另外一个只可以在第二家商店买东西。并且在一个部门不可以同时为两个人同时买东西。

接下来就是发现性质的时候:我们可以知道如果我们选择了x作为第一个朋友的最大值,并且假设这个部门是i。那么之后在\([i+1,n]\)的部门里面。只要存在大于x的第二个就必须得买下来。这个等于是从千万后扫一遍,维护一个后缀的最大值。

但是我们需要注意我们也可以从\([1,i-1]\)个部门里面买,这个也是可以的。所以我们还需要维护一个前缀最大值。

然后我们既然想要两者相差最小,那么肯定得使用二分进行维护了。

// Problem: D. Buying gifts
// Contest: Codeforces - Codeforces Round 857 (Div. 2)
// URL: https://codeforces.com/contest/1802/problem/D
// Memory Limit: 512 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
#define endl "\n"
#define int long long 

void solve()
{
	int n;
	cin>>n;
	vector<pair<int,int>> a(n);
	multiset<int> s1,s2;
	for(int i=0;i<n;i++)
	{
		cin>>a[i].first>>a[i].second;
		s2.insert(a[i].second);
	}
	sort(a.begin(),a.end());
	int ans=1e9;
	// for(auto c:s2)
	// cout<<c<<" ";
	// cout<<endl;
	for(int i=0;i<n;i++)
	{
		auto [x,y]=a[i];
		s2.erase(s2.lower_bound(y));
		if(!s2.empty())
		{
			int mx=*prev(s2.end());
			if(mx>=x)
			ans=min(ans,mx-x);
			else
			{
				ans=min(ans,x-mx);
				auto it=s1.lower_bound(x);
				if(it!=s1.end())
				ans=min(ans,*it-x);
				if(it!=s1.begin())
				ans=min(ans,x-*prev(it));
			}
		}
		else
		{
			auto it=s1.lower_bound(x);
			if(it!=s1.end())//如果等于说明没有比x大的
			ans=min(ans,*it-x);
			if(it!=s1.begin())//如果等于说明没有比x小的.
			ans=min(ans,x-*prev(it));
			
		}
		s1.insert(y);
	}
	cout<<ans<<endl;
}


signed main()
{
int t;
cin>>t;
while(t--)
{
	solve();
}
}
posted @ 2023-03-12 14:18  努力的德华  阅读(55)  评论(0)    收藏  举报