Codeforces Round #496 (Div. 3)

A题

思路:

判断i位是否一定大于i - 1位即可。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;

const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;

int n, m, k;

vi p, ans;

int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << setprecision(10) << fixed;
	cin >> n;
	p.resize(n);
	for(int i = 0; i < n; i++)
		cin >> p[i];
	for(int i = 1; i < n; i++)
		if(p[i] <= p[i - 1])
			ans.push_back(p[i - 1]);
	ans.push_back(p[n - 1]);
	cout << ans.size() << endl;
	for(int i = 0; i < (int)ans.size(); i++){
		if(i)
			cout << " ";
		cout << ans[i];
	}
	cout << endl;
	cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
	return 0;
}


B题

思路:

找最长相同后缀即可。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;

const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;

int n, m, k;

string u, v;

int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << setprecision(10) << fixed;
	cin >> u >> v;
	int same = 0;
	for(int i = u.length() - 1, j = v.length() - 1; i >= 0 && j >= 0; i--, j--){
		if(u[i] == v[j])
			same++;
		else
			break;
	}
	cout << u.length() - same + v.length() - same << endl;
	cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
	return 0;
}


C题

思路:

枚举2 ^ i, 二分找是否相加可以想等。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;

const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;

int n, m, k;

vll p;
vi pass;

int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << setprecision(10) << fixed;
	cin >> n;
	p.resize(n);
	pass.resize(n);
	for(int i = 0; i < n; i++)
		cin >> p[i];
	sort(p.begin(), p.end());
	int ans = 0;
	for(int i = 0; i < n; i++){
		if(pass[i])
			continue;
		bool found = false;
		for(int j = 0; j <= 32; j++){
			ll v = (1 << j);
			if(v <= p[i])
				continue;
			int pos = lower_bound(p.begin(), p.end(), v - p[i]) - p.begin();
			if(pos >= n)
				break;
			while(pos < n && p[i] + p[pos] == v){
				if(pos == i)
					pos++;
				else{
					found = true;
					break;
				}
			}
			if(found){
				pass[pos] = 1;
				break;
			}
		}
		if(!found){
//			cerr << p[i] << endl;
			ans++;
		}	
	}
	cout << ans << endl;
	cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
	return 0;
}


D题

思路:

存前i位能够合成的数的各位之和。

如果能够合成x == 0(mod3)就ans++;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;

const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;

int n, m, k;

string str;

int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << setprecision(10) << fixed;
	cin >> str;
	vi last;
	last.resize(5);
	ll res = 0;
	for(int i = 0; i < (int)str.length(); i++){
		int v = (str[i] - '0') % 3;
		if(!v || last[((v - 1) ^ 1) + 1]){
			last[1] = last[2] = 0;
			res++;
		}
		else{
			if(last[v])
				last[(v + v) % 3] = 1;
			last[v] = 1;
		}
	}
	cout << res << endl;
	cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
	return 0;
}


E1题

思路:

先找到m的位置,然后对右边的m排序,排序的依据是前缀的greater - less(greater指的是严格大于m的数)。

然后从m扫到0, 每次二分在右边找满足(greater - less == 0 || greater = less == 1)的数的总数。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;

const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;

int n, m, k;

vi p, dp;

int cal(int a, int b){
	int res = a - b;
	if(res > 0)
		return 1;
	if(!res)
		return 0;
	return -1;
}

int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << setprecision(10) << fixed;
	cin >> n >> m;
	p.resize(n);
	dp.resize(n);
	int pos;
	for(int i = 0; i < n; i++)
		cin >> p[i];
	for(int i = 0; i < n; i++)
		if(p[i] == m){
			pos = i;
			break;
		}
	dp[0] = cal(p[0], m);
	for(int i = 1; i < n; i++)
		dp[i] = dp[i - 1] + cal(p[i], m);
	int cur = dp[pos];
	sort(dp.begin() + pos, dp.end());
	ll res = 0;
	for(int i = pos; i >= 0; i--){
		cur -= cal(p[i], m);
		int u = lower_bound(dp.begin() + pos, dp.end(), cur) - dp.begin();
		int v = upper_bound(dp.begin() + pos, dp.end(), cur + 1) - dp.begin();
		v--;
		if(dp[v] != cur && dp[v] != cur + 1)
			continue;
		res += (v - u + 1);
	}
	cout << res << endl;
	cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
	return 0;
}


F题

思路:

因为距离都是相等的,所以可以直接bfs来获得最短距离。

把能从上一层次到i层次的路径存给i,最后dfs构造出每个符合的路径。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;

const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;

int n, m, k;

typedef struct star{
	int u, v, nt;
	star(int a, int b, int c){
		u = a;
		v = b;
		nt = c;
	}
}star;

vi head, level;
vii p;
vector<star> st;
bitset<MAXN> pick;

void add(int u, int v){
	st.push_back(star(u, v, head[u]));
	head[u] = st.size() - 1;
}

void recur(int u, ll &sum){
	if(u >= n){
		for(int i = 0; i < 2 * m; i += 2){
			if(pick[i] || pick[i ^ 1])
				cout << 1;
			else
				cout << 0;
		}
		cout << endl;
		sum++;
		return;
	}
	for(int i = 0; i < (int)p[u].size(); i++){
		int cur = p[u][i];
		pick[cur] = 1;
		recur(u + 1, sum);
		if(sum >= k)
			return;
		pick[cur] = 0;
	}
	return;
}

int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << setprecision(10) << fixed;
	cin >> n >> m >> k;
	head.resize(n, -1);
	p.resize(n, vi(0));
	for(int i = 0; i < m; i++){
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		add(u, v);
		add(v, u);
	}
	level.resize(n);
	queue<int> q;
	q.push(0);
	level[0] = 1;
	while(!q.empty()){
		int u = q.front();
		q.pop();
		for(int i = head[u]; ~i; i = st[i].nt){
			int v = st[i].v;
			if(level[v]){
				if(level[v] == level[u] + 1)
					p[v].push_back(i);
				continue;
			}
			level[v] = level[u] + 1;
			p[v].push_back(i);
			q.push(v);
		}
	}
	ll res = 1;
	for(int i = 1; i < n; i++){
		res *= p[i].size();
		if(res >= k)
			break;
	}
	k = min(res, (ll)k);
	cout << k << endl;
	res = 0;
	recur(1, res);
	cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
	return 0;
}

未来可期。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值