Codeforces Round #737 (Div. 2)

A. Ezzat and Two Subsequences

水题 把最大的那个拿出来 其他的求平均值即可 可以自己算一下 a1, a2, a3, a4 找最优解

代码

#include <bits/stdc++.h>
 
using namespace std;

typedef long long ll;
 
const int N = 3e5 + 10;
 
inline ll read()
{
	register ll x = 0, k = 1;
	char c = getchar();
	while (c > '9' || c < '0') 
	{
		if (c == '-') k = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	return x * k;
}
 
ll n, a[N];
 
void solve()
{
	ll sum = 0;
	n = read();
	for (int i = 1; i <= n; i ++ ) a[i] = read(), sum += a[i];
	sort(a + 1, a + n + 1); sum -= a[n];
	double ans = (double)sum * 1.0 / (n - 1) + (double)a[n];
	printf("%.9lf\n", ans);
}
 
int main()
{
	int t;
	t = read();
 
	while (t -- )
	{
		solve();
	}
 
	return 0;
}

B. Moamen and k-subarrays

水题 用一个map记录下排序后的顺序 看原数组哪段的前后元素顺序差不为1

代码

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 1; i <= (n); i ++ )

using namespace std;

typedef long long ll;

const int N = 3e5 + 10;

inline ll read()
{
	register ll x = 0, k = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') k = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	return x * k;
}

ll n, k;
ll a[N], b[N];
map<ll, int> mp;

void solve()
{
 	n = read(), k = read();
 	rep(i, n){a[i] = read(); b[i] = a[i];}

 	sort(b + 1, b + 1 + n);
 	rep(i, n) mp[b[i]] = i;

 	int cnt = 1;
 	rep(i, n){
 		if (i == 1) continue;
 		if (mp[a[i]] != mp[a[i - 1]] + 1) cnt ++ ;
 	}
 	if (cnt <= k) puts("YES");
 	else puts("NO");
}

int main()
{
	ll t;
	t = read();

	while (t -- )
	{
		solve();
	}

	return 0;
}

C. Moamen and XOR

当n为偶数时 只要每位1的个数都为偶数 则相等 (n为偶数时 与值是一定小于等于异或值的)
当n为奇数时 当某位全为1则地位可以随便取 高位要取偶数位的1

代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 3e5 + 10, P = 1e9 + 7;

inline ll read()
{
	register ll x = 0, k = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') k = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	return x * k;
}

ll qpow(ll x, ll k)
{
	ll ans = 1;
	while (k)
	{
		if (k & 1) ans = (ans * x) % P;
		x = (x * x) % P;
		k >>= 1;
	}
	return ans % P;
}

ll n, k;

void solve()
{
	n = read(), k = read();
	ll ans = 1;
	if (k == -1) {puts("1"); return;}

	if (n % 2){
		ans = (qpow(2, n - 1) + 1) % P;
		ans = qpow(ans, k);
	}
	else{
		// 二者相同
		ans = (qpow(2, n - 1) - 1 + P) % P; 
		ans = qpow(ans, k);

		// 二者不同
		for (int i = 1; i <= k; i ++ )
		{
			int pre = k - i;
			ll res = (qpow(2, n - 1) - 1 + P) % P;
			res = qpow(res, pre);
			int bac = i - 1;
			res = (res * qpow(qpow(2, n), bac)) % P;
			(ans += res) %= P;
		}
	}
	cout << ans << endl;
}	

int main()
{
	int t;
	t = read();

	while (t -- )
	{
		solve();
	}

	return 0;
}

D - Ezzat and Grid

线段树加dp 用wmax来维护当前行的最大值以及是从哪一行变换过来的
离散化一下数据即可

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 3e5 + 10;

inline int read()
{
	register int x = 0, k = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') k = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return x * k;
}

int n, m;
vector<int> alls;
vector<PII> g[N];
int path[N], ans[N];

int find(int x)
{
	return lower_bound(alls.begin(), alls.end(), x) - alls.begin() + 1;
}

struct Node
{
	int l, r, lazy;
	PII wmax;
}tr[N << 3];

void pushup(int u)
{
	tr[u].wmax = max(tr[u << 1].wmax, tr[u << 1 | 1].wmax);
}

void pushdown(int u)
{
	if (tr[u].lazy)
	{
		tr[u << 1].wmax = tr[u << 1 | 1].wmax = tr[u].wmax;
		tr[u << 1].lazy = tr[u << 1 | 1].lazy = 1;
		tr[u].lazy = 0;
	}
	return;
}

void build(int u, int l, int r)
{
	tr[u] = {l, r, 0, {0, -1}};
	if (l == r) return;
	int mid = l + r >> 1;
	build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r);
	pushup(u);
}

PII query(int u, int l,  int r)
{
	if (tr[u].l >= l && tr[u].r <= r) return tr[u].wmax;
	pushdown(u);
	int mid = tr[u].l + tr[u].r >> 1;
	PII res = {0, -1};
	if (l <= mid) res = max(res, query(u << 1, l, r));
	if (r > mid) res = max(res, query(u << 1 | 1, l, r));
	return res;
}

void modify(int u, int l, int r, PII x)
{
	if (tr[u].l >= l && tr[u].r <= r)
	{
		tr[u].lazy = 1;
		tr[u].wmax = x;
		return;
	}
	int mid = tr[u].l + tr[u].r >> 1;
	pushdown(u);
	if (l <= mid) modify(u << 1, l, r, x);
	if (r > mid) modify(u << 1 | 1, l, r, x);
	pushup(u);
}

void solve()
{
	n = read(), m = read();
	for (int i = 0; i < m; i ++ )
	{
		int id, l, r; 
		id = read(), l = read(), r = read();
		g[id].push_back(make_pair(l, r));
		alls.push_back(l), alls.push_back(r);
	}

	//离散化
	sort(alls.begin(), alls.end());
	alls.erase(unique(alls.begin(), alls.end()), alls.end());
	int cnt = alls.size();

	for (int i = 1; i <= n; i ++ )
		for (int j = 0; j < g[i].size(); j ++ )
		{
			g[i][j].first = find(g[i][j].first);
			g[i][j].second = find(g[i][j].second);
		}

	build(1, 1, cnt);

	for (int i = 1; i <= n; i ++ )
	{
		PII maxn = {0, -1};
		for (int j = 0; j < g[i].size(); j ++ ) maxn = max(maxn, query(1, g[i][j].first, g[i][j].second));
		path[i] = maxn.second;
		maxn.first ++, maxn.second = i;
		for (int j = 0; j < g[i].size(); j ++ ) modify(1, g[i][j].first, g[i][j].second, maxn);

	}

	PII mx = query(1, 1, cnt);
	printf("%d\n", n - mx.first);
	int now = mx.second;
	while (now != -1)
	{
		ans[now] = 1;
		now = path[now];
	}
	for (int i = 1; i <= n; i ++ ) if (!ans[i]) printf("%d ", i);
}

int main()
{
	solve();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值