Codeforces Round #843 (Div. 2) A1 —— D

题目地址:Dashboard - Codeforces Round #843 (Div. 2) - Codeforces
一个不知名大学生,江湖人称菜狗
original author: jacky Li
Email : 3435673055@qq.com

Time of completion:2023.1.11
Last edited: 2023.1.11

目录

​编辑

A1. Gardener and the Capybaras (easy version)

思路

参考代码

A2. Gardener and the Capybaras (hard version)

思路

参考代码

B. Gardener and the Array

思路

参考代码

C. Interesting Sequence

思路

参考代码

D. Friendly Spiders

思路

参考代码


A1. Gardener and the Capybaras (easy version)

思路

简单版本的仅需要灵活运用字符串进行暴力搜索就行,因为每一个字符串最大仅有100个

PS:当然运用A2肯定也是可以过掉的。

参考代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 5e7+5, K = 2e5+10, Z = 1e5+7, X = 1.5 * 1e9;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI;
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

void solve()
{
	string ch; cin >> ch;
	int n = ch.size();
//	cout << "n : " << n << endl;
//	cout << "substr(2, 1)" << str.substr(2, 1) << endl;
	ch = ' ' + ch;
	for(int i = 1; i < n; i ++)
		for(int j = i + 1; j < n; j ++ )
		{
			string a = ch.substr(1, i), b = ch.substr(i + 1, j - i), c = ch.substr(j + 1, n - j);
			
			if((a <= b && c <= b) || (a >= b && c >= b))
			{
				cout << a << ' ' << b << ' ' << c << endl;
				return;
			}
		}
}

signed main()
{
    IOS; int T = 1;
	cin.tie(nullptr); 
	cout.tie(nullptr);
    cin >> T;
    while( T -- ) solve();
    return 0;
}

A2. Gardener and the Capybaras (hard version)

思路

用A1 的方法也可以写但是由于困难版本的数据最大到达了2e5的一个程度,所以用A1的方法难免会tle

因此我们思考发现仅有两种情况

①xxxxx  bxxxxx x

②xxxx a xxxxx

参考代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define NO cout << "NO" << endl
#define int long long
//#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 1e6+5, K = 1e5+10, Z = 2e5+7;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI; 
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

int n, m, t = 0, cnt;
char ch[5002], str[5002], a[5002];
bool flag = false;

void solve()
{
	string ch; cin >> ch;
	int t = ch.size();
	ch = ' ' + ch;
	string b;
	for(int i = 2; i < t; i ++)
		if(ch[i] == 'a')
		{ 
			b = ch[i];
			cout << ch.substr(1, i - 1) << ' ' << b << ' ' << ch.substr(i + 1, t - i) << endl;
			return;
		}
	for(int i = 2; i < t; i ++)
	{
		if(ch[i] == 'b')
		{
			cout << ch.substr(1, i - 1) << ' ' << ch.substr(i, t - i) << ' ' << ch[t] << endl;
			return;
		}
	}
}

signed main()
{
    IOS; int T = 1;
    cin >> T;
    while( T -- ) solve();
    return 0;
}

B. Gardener and the Array

思路

存入一个map<int, int> 的一个东西,通过输出判断是否为0,博主 笨看代码吧

参考代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 5e7+5, K = 2e5+10, Z = 1e5+7, X = 1.5 * 1e9;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI; 
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

vector<int> s[N];

void solve()
{
	int n; cin >> n;
	map<int, int> jym;
	for(int i = 1; i <= n; i ++ ) s[i].clear();

	for(int i = 1; i <= n; i ++ )
	{
		int a; cin >> a;
		
		for(int j = 1; j <= a; j ++)
		{
			int x; cin >> x;
			s[i].push_back(x);
			jym[x] = jym[x] + 1;
		}
	}
	
	for(int i = 1; i <= n; i ++)
	{
		bool lcq = true;
		for(auto x : s[i])
			if(jym[x] == true) {lcq = 0; break;}
		
		if(lcq)
		{
			YES; return;
		}
	}
	
	NO;
}

signed main()
{
    IOS; int T = 1;
	cin.tie(nullptr); 
	cout.tie(nullptr);
    cin >> T;
    while( T -- ) solve();
    return 0;
}

C. Interesting Sequence

思路

首先如果模式串中有某一位2进制为0需要得到1,一定不可能

其次 n某一位为1,x某一位为0,把n变为x,n-m肯定有一位这位为0的,找到最小的这一位为0的,和其他的取max

参考代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

#define IOS std::ios::sync_with_stdio(false)
#define inf 9e18
#define YES cout << "YES" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 1e6+5, K = 1e5+10, Z = 2e5+7, X = 9e18+2;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI; 
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

//bool check(int n, int x)
//{
//	for(int i = 0; i < 60; i ++ )
//	{
//		int cnt_1 = (n >> i) & 1, cnt_2 = (x >> i) & 1;
//		if(cnt_1 == 0 && cnt_2 == 1)
//			return false;
//	}
//	return true;
//}

void solve()
{
	int n, x; cin >> n >> x;
	
	for(int i = 0; i < 60; i ++ )
		{
			int cnt_1 = (n >> i) & 1, cnt_2 = (x >> i) & 1;
			if(cnt_1 == 0 && cnt_2 == 1)
			{
				cout << -1 << endl;
				return;
			}
		}
//	if(check(n, x) == false) cout << -1 << endl; 
//	else
//	{
		int jym_x = n, jym_n = X, ans1 = 0, ans2 = n;
		
			for(int i = 60; i >= 0; i -- )
			{
				int cnt_1 = n >> i & 1, cnt_2 = x >> i & 1;
				
				if(cnt_1 == 1 && cnt_2 == 0) jym_x = max(jym_x, ans1 + (1LL << i) * 2);
				else if(cnt_1 == 1 && cnt_2 == 1) jym_n = min(jym_n, ans1 + (1LL << i) * 2);
				if(n >> i & 1) ans1 += 1LL << i, ans2 -= 1LL << i;
				
			}
			
			if(jym_x >= jym_n) cout << -1 << endl;
			else cout << jym_x << endl;
//	}
}

signed main()
{
    IOS; int T = 1;
	cin.tie(nullptr); 
	cout.tie(nullptr);
    cin >> T;
    while( T -- ) solve();
    return 0;
}

D. Friendly Spiders

思路

运用迪杰斯特拉算法,当最后还超过某一个巨大的数的时候则是-1

参考代码

// 参考的友友的
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
#define cmp [&](PII a, PII b){return a.y < b.y;}

const int N = 5e5+10, mod = 1e9+7, M = 5e7+5, K = 2e5+10, Z = 1e5+7, X = 1.5 * 1e9;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI;
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

int n, m, k;
int s, t;

vector<int> g[N], v[N], c;
int a[N], b[N];
int d[N], pre[N];
bool st[N];

void dijkstra()
{
	for(int i = 1; i <= n; i ++ ) d[i] = 1e9;
	deque<PII> q;
	d[s] = 1;
	q.push_front({1, s});
	
	while(q.size())
	{
		PII t = q.front();
		q.pop_front();
		
		
		if(st[t.y]) continue;
		st[t.y] = 1;
		
		for(auto x : v[t.y])
		{
			int f = t.y <= k;
			if(d[x] > t.x + f)
			{
				d[x] = t.x + f;
				if(f) q.push_front({d[x], x});
				else q.push_back({d[x], x});
				pre[x] = t.y;
			}
		}
	}
}


void print(int x)
{
	if(x <= k) c.push_back(x);
	if(x == s) return;
	print(pre[x]);
}

void solve()
{
	cin >> n;
	k = n;
	set<int> S;
	for(int i = 1; i <= n; i ++ ) 
	{
		cin >> a[i];
		int x = a[i];
		for(int j = 2; j * j <= x; j ++ )
		{
			if(x % j == 0) g[i].push_back(j), S.insert(j);
			while(x % j == 0) x /= j;
		}
		if(x > 1) g[i].push_back(x), S.insert(x);
	}
	
	cin >> s >> t;
	
	for(auto x : S) b[x] = ++ n;
	
	for(int i = 1; i <= n; i ++ )
	{
		for(auto x : g[i])
		{
			v[i].push_back(b[x]);
			v[b[x]].push_back(i);
		}
	}
	
	dijkstra();
	if(d[t] >= 1e9 / 2) cout << -1 << endl;
	else
	{
		cout << d[t] << endl;
		print(t);
		reverse(c.begin(), c.end());
		for(auto x : c) cout << x << ' ';
	}
}

signed main()
{
    IOS; int T = 1;
	cin.tie(nullptr); 
	cout.tie(nullptr);
    cin >> T;
    while( T -- ) solve();
    return 0;
}

作者有言

如果感觉博主讲的对您有用,请点个关注支持一下吧,将会对此类问题持续更新……

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

羁旅少年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值