Codeforces Round 889 (Div. 2)(视频讲解A——D)

文章目录

  • [A Dalton the Teacher](#A Dalton the Teacher)
  • [B Longest Divisors Interval](#B Longest Divisors Interval)
  • [C2 Dual (hard Version)](#C2 Dual (hard Version))
  • [D Earn or Unlock](#D Earn or Unlock)

Codeforces Round 889 (Div. 2)(视频讲解A------D)

A Dalton the Teacher

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 1e5 + 10;

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

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	solve();
}

B Longest Divisors Interval

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 1e5 + 10;

void solve()
{
	int n;
	cin >> n;
	if(n <= 2)
	{
		cout << n << endl;
		return;
	}
	for(int i = 1; i <= n; i ++)
	{
		if((n % i) != 0)
		{
			cout << i - 1 << endl;
			return;
		}
	}
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	solve();
}

C2 Dual (hard Version)

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 1e5 + 10;

void add_1(vector<int>&a, vector<pii>&ans)
{
	for(int i = 0; i < a.size() - 1; i ++)
	{
		a[i + 1] += a[i];
		ans.push_back({i + 1, i});
	}
	cout << ans.size() << endl;
	for(auto [a, b] : ans)
		cout << a + 1 << " " << b + 1 << endl;
	return;
}

void add_2(vector<int>&a, vector<pii>&ans)
{
	for(int i = a.size() - 1; i >= 1; i --)
	{
		a[i - 1] += a[i];
		ans.push_back({i - 1, i});
	}
	cout << ans.size() << endl;
	for(auto [a, b]: ans)
		cout << a + 1 << " " << b + 1 << endl;
	return;
}

void solve()
{
	int n;
	cin >> n;
	vector<int>a(n);
	int p_max = 0, p_min = 0;
	int cnt_1 = 0, cnt_2 = 0;
	vector<pii>ans;
	for(int i = 0; i < n; i ++)
	{
		cin >> a[i];
		if(a[p_max] < a[i])
			p_max = i;
		if(a[p_min] > a[i])
			p_min = i;
		if(a[i] < 0)
			cnt_2 ++;
		if(a[i] > 0)
			cnt_1 ++;
	}
	
	if(a[p_min] >= 0)
	{
		add_1(a, ans);
		return;
	}
	
	if(a[p_max] <= 0)
	{
		add_2(a, ans);
		return;
	}

	if(abs(a[p_max]) >= abs(a[p_min]))
	{
		if(cnt_2 <= 12)
		{
			for(int i = 0; i < n; i ++)
			{
				if(a[i] < 0)
				{
					a[i] += a[p_max];
					ans.push_back({i, p_max});
				}
			}
			add_1(a, ans);
		}
		else
		{
			for(int i = 0; i < 5; i ++)
			{
				if(abs(a[p_min]) >= abs(a[p_max]))
					break;
				ans.push_back({p_min, p_min});
				a[p_min] += a[p_min];
			}

			for(int i = 0; i < n; i ++)
			{
				if(a[i] > 0)
				{
					a[i] += a[p_min];
					ans.push_back({i, p_min});
				}
			}
			add_2(a, ans);
		}
	}
	else
	{
		if(cnt_1 <= 12)
		{
			for(int i = 0; i < n; i ++)
			{
				if(a[i] > 0)
				{
					a[i] += a[p_min];
					ans.push_back({i, p_min});
				}
			}
			add_2(a, ans);
		}
		else
		{
			for(int i = 0; i < 5; i ++)
			{
				if(abs(a[p_max]) >= abs(a[p_min]))
					break;	
				a[p_max] += a[p_max];
				ans.push_back({p_max, p_max});
			}
			for(int i = 0; i < n; i ++)
			{
				if(a[i] < 0)
				{
					a[i] += a[p_max];
					ans.push_back({i, p_max});
				}
			}
			add_1(a, ans);
		}
	}


}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	solve();
}

D Earn or Unlock

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int N = 2e5 + 10;
bitset<N>f;
int n, a[N], sum[N];
void solve()
{
	cin >> n;
	for(int i = 1; i <= n; i ++)
		cin >> a[i];
	for(int i = 1; i <= 2 * n; i ++)
		sum[i] = sum[i - 1] + a[i];

	int ans = 0;
	f[1] = 1;
	for(int i = 1; i <= 2 * n; i ++)
	{
		f |= (f << a[i]);
		if(f[i])
		{
			ans = max(sum[i] - i + 1, ans);
			f[i] = 0;
		}
	}
	cout << ans << endl;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	solve();
}
相关推荐
十五年专注C++开发1 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译
森焱森2 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
QuantumStack4 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
天若有情6734 小时前
01_软件卓越之道:功能性与需求满足
c++·软件工程·软件
whoarethenext4 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
写个博客4 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠4 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
hie988945 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
Jay_5155 小时前
C++多态与虚函数详解:从入门到精通
开发语言·c++
杰克尼5 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表