首页 > 其他分享 >Codeforces Round 971 (Div. 4)题解记录(G3待补)

Codeforces Round 971 (Div. 4)题解记录(G3待补)

时间:2024-09-26 18:15:06浏览次数:1  
标签:G3 待补 题解 ll cin ans return include mod

A. Minimize!

暴力模拟一遍即可

#include<iostream>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<bitset>
#include<math.h>
#include<random>
#include<string>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
const ll p = rnd();
ll ksm(ll x, ll y) { ll ans = 1; while (y) { if (y & 1)ans = (ans % mod * (x % mod)) % mod; x = (x % mod) * (x % mod) % mod % mod; y >>= 1; }return ans % mod % mod; }
ll gcd(ll x, ll y) { if (y == 0)return x; else return gcd(y, x % y); }
void fio() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
int main()
{
ll t;
cin>>t;
while(t--)
{
	ll a,b;
	cin>>a>>b;
	ll zm=1561616154;
	for(ll i=a;i<=b;i++)
	{
		zm=min(zm,i-a+b-i);
	}
	cout<<zm<<endl;
} 
}

B. osu!mania

暴力模拟

#include<iostream>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<bitset>
#include<math.h>
#include<random>
#include<string>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
const ll p = rnd();
ll ksm(ll x, ll y) { ll ans = 1; while (y) { if (y & 1)ans = (ans % mod * (x % mod)) % mod; x = (x % mod) * (x % mod) % mod % mod; y >>= 1; }return ans % mod % mod; }
ll gcd(ll x, ll y) { if (y == 0)return x; else return gcd(y, x % y); }
void fio() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
int main()
{
ll t;
cin>>t;
while(t--)
{
	ll n;
	cin>>n;
	vector<ll>g;
	string f[600];
	for(ll i=1;i<=n;i++)
	{
		cin>>f[i];
	}
	for(ll i=n;i>=1;i--)
	{
		for(ll j=0;j<=3;j++)
		{
			if(f[i][j]=='#')
			{
				g.push_back(j+1);
				break;
			}
		}
	}
	for(auto j:g)
	{
		cout<<j<<" ";
	}
	cout<<endl;
}
}

C. The Legend of Freya the Frog

最大值其实取决于你什么方向最晚到目标位置
如果是水平方向更大直接两倍减一
如果是竖直方向直接两倍即可

#include<iostream>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<bitset>
#include<math.h>
#include<random>
#include<string>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
const ll p = rnd();
ll ksm(ll x, ll y) { ll ans = 1; while (y) { if (y & 1)ans = (ans % mod * (x % mod)) % mod; x = (x % mod) * (x % mod) % mod % mod; y >>= 1; }return ans % mod % mod; }
ll gcd(ll x, ll y) { if (y == 0)return x; else return gcd(y, x % y); }
void fio() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
int main()
{
ll t;
cin>>t;
while(t--)
{
ll x,y,k;
cin>>x>>y>>k;
ll ans=x/k+(x%k>0);
ll cnt=y/k+(y%k>0);
if(cnt<ans)
{
	cout<<ans+ans-1<<endl;
}
else
{
	cout<<cnt+cnt<<endl;
}
}
}

D. Satyam and Counting

当直角三角形的直角边不垂直水平方向,直接特判计数
否则直接考虑除了上下两个点的组合数情况

#include<iostream>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<bitset>
#include<math.h>
#include<random>
#include<string>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
const ll p = rnd();
ll ksm(ll x, ll y) { ll ans = 1; while (y) { if (y & 1)ans = (ans % mod * (x % mod)) % mod; x = (x % mod) * (x % mod) % mod % mod; y >>= 1; }return ans % mod % mod; }
ll gcd(ll x, ll y) { if (y == 0)return x; else return gcd(y, x % y); }
void fio() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
map<pair<ll,ll>,ll>q;
int main()
{
ll t;
cin>>t;
while(t--)
{
	q.clear();
	ll n;
	cin>>n;
	ll ans=0;
	for(ll i=1;i<=n;i++)
	{
		ll x,y;
		cin>>x>>y;
		q[{x,y}]++;
		if(y==0)
		{
			if(q[{x,1}]>0)
			ans+=n-2;
		}
		else
		{
			if(q[{x,0}]>0)
			ans+=n-2;
		}
		if(y==0)
		{
			if(q[{x-1,1}]&&q[{x+1,1}])
			ans++;
			if(q[{x-1,1}]&&q[{x-2,0}])
			ans++;
			if(q[{x+1,1}]&&q[{x+2,0}])
			ans++;
		}
		else
		{
			if(q[{x-1,0}]&&q[{x+1,0}])
			ans++;
			if(q[{x+1,0}]&&q[{x+2,1}])
			ans++;
			if(q[{x-1,0}]&&q[{x-2,1}])
			ans++;
		}
	}
	cout<<ans<<endl;
}
}

E. Klee 的 SUPER DUPER LARGE Array!!

直接二分

#include<iostream>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<bitset>
#include<math.h>
#include<random>
#include<string>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
const ll p = rnd();
ll ksm(ll x, ll y) { ll ans = 1; while (y) { if (y & 1)ans = (ans % mod * (x % mod)) % mod; x = (x % mod) * (x % mod) % mod % mod; y >>= 1; }return ans % mod % mod; }
ll gcd(ll x, ll y) { if (y == 0)return x; else return gcd(y, x % y); }
void fio() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
map<pair<ll,ll>,ll>q;
int main()
{
ll t;
cin>>t;
while(t--)
{
	ll n,k;
	cin>>n>>k;
	ll l=1,r=n;
	ll sum=(k+k+n-1)*n/2;
	while(l<=r)
	{
		ll mid=(l+r)>>1;
		ll u=(k+k+mid-1)*mid/2;
		if(u>=sum-u)
		{
			r=mid-1;
		}
		else
		l=mid+1;
	}
	if(l>1)
	{
		cout<<min(abs((k+(l-2)+k)*(l-1)/2-(sum-(k+(l-2)+k)*(l-1)/2)),abs((k+l-1+k)*l/2-(sum-(k+l-1+k)*l/2)))<<endl;
	}
	else
	cout<<abs((k+l-1+k)*l/2-(sum-(k+l-1+k)*l/2))<<endl;
}
}

F. Firefly's Queries

这里其实数值和是存在一个周期的,直接左右两边尽量缩减然后形成周期的倍数比较好做
本人直接正向暴力了,\(WA\)了三发过

#include<iostream>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<bitset>
#include<math.h>
#include<random>
#include<string>
#include<string.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
const ll p = rnd();
ll ksm(ll x, ll y) { ll ans = 1; while (y) { if (y & 1)ans = (ans % mod * (x % mod)) % mod; x = (x % mod) * (x % mod) % mod % mod; y >>= 1; }return ans % mod % mod; }
ll gcd(ll x, ll y) { if (y == 0)return x; else return gcd(y, x % y); }
void fio() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
ll a[250000];
ll n;
ll ans = 0;
ll b[250000];
ll dfs(ll x, ll y)
{
	if (x > y)
		return  15666516;
	//	cout<<x<<" "<<y<<endl;
	ll y1, x1;
	ll u = x / n;//周期数
	ll m = x % n;
	if (x % n == 0)//刚好是这个周期得最后一个
	{
		u--;
		u %= n;
		if (u == 0)//第一周期
			x1 = n;
		else
			x1 = u;
	}
	else
		x1 = u % n + 1;
	u %= n;
	//	cout<<u<<endl;
	if (u == 0)//第0周期特判 
		y1 = n;
	else
		y1 = u;//周期数
	// y1为右约束
	ll g;
	if (y1 >= x1)
		g = 1;
	else
		g = 0;
	ll cnt = 0;
	//	cout<<x1<<" "<<y1<<endl;
	cnt = m;
	if (g == 1)
		x1 += max(cnt-1 , (ll)0);
	else
	{
		if (x1 + cnt-1> n)
			x1 = (x1 + cnt-1) % n,g=1;
		else
			x1 = x1 + cnt-1;
	}
	//cout<<x1<<" "<<y1<<endl;
	ll dis = y - x + 1;
	if (g == 1)
	{
		ans += b[min(x1 + dis - 1, y1)] - b[x1 - 1];
		x += min(dis, y1 - x1 + 1);
	}
	else
	{
		ans += b[min(x1 + dis - 1, n)] - b[x1 - 1];
		x += min(dis, n - x1 + 1);
		x1 = 1;
		if (x <= y)
		{
			dis = y - x + 1;
			ans += b[min(x1 + dis - 1, y1)] - b[x1 - 1];
			x += min(dis, y1 - x1 + 1);
		}
	}
	return x;
}
int main()
{
	fio();
	ll t;
	cin >> t;
	while (t--)
	{
		ll q;
		cin >> n >> q;
		//	cout<<n<<endl;
		ll sum = 0;
		b[0] = 0;
		for (ll i = 1; i <= n; i++)
		{
			cin >> a[i];
			sum += a[i];
			b[i] = b[i - 1] + a[i];
		}
		while (q--)
		{
			ans = 0;
			ll l1, r1;
			cin >> l1 >> r1;
			l1 = dfs(l1, r1);
			//	cout<<l1<<endl;
			if (l1 > r1)
				cout << ans << endl;
			else
			{
				ll l = 0, r = n;
				while (l < r)
				{
					ll mid = (l + r) >> 1;
					if (l1 + mid * n - 1 >= r1)
					{
						r = mid;
					}
					else
						l = mid + 1;
				}
				if (l1 + r * n - 1 == r1)
				{
					cout << ans + r * sum << endl;
				}
				else
				{
					r--;
					ans += r * sum;
					l1 = l1 + r * n - 1;
					l1++;
					ll u = dfs(l1, r1);
					cout << ans << endl;
				}
			}
		}
	}
}

G1. Yunli's Subarray Queries (easy version)

求最长连续的数得先处理一下,直接减(1,2,3,...),因为连续且增大得数差值会变为同一个值,方便统计
然后维护队列加数组记录,这里加了个大数,使得负数情况消失。空间换时间

#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<bitset>
#include<stack>
#include<algorithm>
#include<deque>
using namespace std;
typedef long long ll;
ll a[200005];
ll b[200005];
ll e[400005];//桶排序
ll u[200005];
void fio()
{
	ios::sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
}
int main()
{
	fio();
	ll t;
	cin >> t;
	while (t--)
	{
		priority_queue<pair<ll,ll>>q;
		ll n, k, m;
		cin >> n >> k >> m;
		for (ll i = 1; i <= n; i++)cin >> a[i];
		for (ll i = 1; i <= n; i++)
		{
			b[i] = a[i] - i+200000;//偏差值
		}
		ll l = 0, r = 0;
		for (ll i = 1; i <= n; i++)
		{
			if (l == 0 && r == 0)
			{
				l = r = i;
				e[b[i]]++;//桶排序
				q.push({ e[b[i]],b[i] });
				if (r - l + 1 == k)
					u[l] = q.top().first;
			}
			else
			{
				if (r - l + 1 < k)
				{
					r++;
					e[b[i]]++;
					while (!q.empty() && q.top().first != e[q.top().second])
					{
						q.pop();
					}
					q.push({ e[b[i]],b[i] });
					if (r - l + 1 == k)
						u[l] = q.top().first;
				}
				else if (r - l + 1 == k)
				{
					e[b[l]]--;
					e[b[i]]++;
					while (!q.empty() && q.top().first != e[q.top().second])
					{
						q.pop();
					}
					if (e[b[l]] != 0)
					{
						q.push({ e[b[l]],b[l] });
					}
					q.push({ e[b[i]],b[i] });
					l++;
					r++;
					u[l] = q.top().first;
				}
			}
		}
		for (ll i = 1; i <= n; i++)
		{
			e[b[i]] = 0;
		}
		while (m--)
		{
			ll l, r;
			cin >> l >> r;
			cout << k-u[l] << endl;
		}
	}
}

G2. Yunli's Subarray Queries (hard version)

这里在G1得基础上用了堆栈+线段树
对照过正解,理应后面两题得用莫队做

#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<bitset>
#include<stack>
#include<algorithm>
#include<deque>
using namespace std;
typedef long long ll;
ll a[200005];
ll b[200005];
ll e[400005];//桶排序
ll u[200005];
ll f[200005];
vector<pair<ll,ll>>g[200005];
struct s
{
	ll l, r;
	ll v, lazy;
}p[250000<<2];
void build(ll i, ll l, ll r)
{
	p[i].l = l;
	p[i].r = r;
	p[i].v=0;
	p[i].lazy = -1;
	if (l == r)
	{
		return;
	}
	build(i << 1, l, (l + r) >> 1);
	build(i << 1 | 1, ((l + r) >> 1)+1, r);
}
void pushdown(ll i)
{
	if (p[i].lazy>=0)
	{
		ll i1 = i << 1, i2 = i << 1 | 1;
		p[i1].v = (p[i1].r - p[i1].l + 1) * p[i].lazy;
		p[i2].v = (p[i2].r - p[i2].l + 1) * p[i].lazy;
		p[i1].lazy = p[i].lazy;
		p[i2].lazy = p[i].lazy;
		p[i].v = p[i1].v + p[i2].v;
		p[i].lazy = -1;
	}
}
void ud(ll i, ll l, ll r,ll x)
{
	if (l == p[i].l && r == p[i].r)
	{
		p[i].v = (p[i].r - p[i].l + 1) * x;
		p[i].lazy = x;
		return;
	}
	pushdown(i);
	ll i1 = i << 1;
	ll i2 = i << 1 | 1;
	if (l <= p[i1].r)
	{
		if (r <= p[i1].r)
		{
			ud(i1, l, r, x);
		}
		else
			ud(i1, l, p[i1].r, x);
	}
	if (r >= p[i2].l)
	{
		if (l >= p[i2].l)
			ud(i2, l, r, x);
		else
			ud(i2, p[i2].l, r, x);
	}
	p[i].v = p[i1].v + p[i2].v;
}
ll query(ll i, ll l, ll r)
{
	ll ans = 0;
	if (l == p[i].l && r == p[i].r)
	{
		ans+=p[i].v;
		return ans;
	}
	pushdown(i);
	ll i1 = i<< 1;
	ll i2 = i << 1 | 1;
	if (l <= p[i1].r)
	{
		if (r <= p[i1].r)
			ans += query(i1, l, r);
		else
			ans += query(i1, l, p[i1].r);
	}
	if (r >= p[i2].l)
	{
		if (l >= p[i2].l)
			ans += query(i2, l, r);
		else
			ans += query(i2, p[i2].l, r);
	}
	return ans;
}
void fio()
{
	ios::sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
}
int main()
{
	fio();
	ll t;
	cin >> t;
	build(1, 1, 200020);
	while (t--)
	{
		priority_queue<pair<ll, ll>>q;
		ll n, k, m;
		cin >> n >> k >> m;
		ud(1, 1, n, 0);
		for (ll i = 1; i <= n; i++)cin >> a[i];
		for (ll i = 1; i <= n; i++)
		{
			b[i] = a[i] - i + 200000;//偏差值
		}
		ll l = 0, r = 0;
		for (ll i = 1; i <= n; i++)
		{
			if (l == 0 && r == 0)
			{
				l = r = i;
				e[b[i]]++;//桶排序
				q.push({ e[b[i]],b[i] });
				if (r - l + 1 == k)
					u[l] = q.top().first;
			}
			else
			{
				if (r - l + 1 < k)
				{
					r++;
					e[b[i]]++;
					while (!q.empty() && q.top().first != e[q.top().second])
					{
						q.pop();
					}
					q.push({ e[b[i]],b[i] });
					if (r - l + 1 == k)
						u[l] = q.top().first;
				}
				else if (r - l + 1 == k)
				{
					e[b[l]]--;
					e[b[i]]++;
					while (!q.empty() && q.top().first != e[q.top().second])
					{
						q.pop();
					}
					if (e[b[l]] != 0)
					{
						q.push({ e[b[l]],b[l] });
					}
					q.push({ e[b[i]],b[i] });
					l++;
					r++;
					u[l] = q.top().first;
				}
			}
		}//处理好了
		for (ll i = 1; i <= n; i++)
		{
			e[b[i]] = 0;
		}
		for (ll i = 1; i <= m; i++)
		{
			ll l, r;
			cin >> l >> r;
			g[l].push_back({ r - k + 1,i });;
		}
		stack<pair<ll,ll>>q1;
		for (ll i = n-k+1; i >= 1; i--)
		{
			u[i] = k - u[i];
			if (q1.empty())
			{
				q1.push({ i,u[i] });
				ud(1, i, i, u[i]);
			}
			else
			{
				ll cnt = i;
				while (!q1.empty() && q1.top().second > u[i])
				{
					cnt = q1.top().first;
					q1.pop();
				}
				ud(1, i, cnt, u[i]);
				q1.push({ cnt,u[i] });
			}
			for (auto j : g[i])
			{
				f[j.second] = query(1, i, j.first);
			}
			g[i].clear();
		}
		for (ll i = 1; i <= m; i++)
		{
			cout << f[i] << endl;
		}
	}
}

G3. Yunli's Subarray Queries (extreme version)

很久没写过莫队了,改天有激情时再补充

标签:G3,待补,题解,ll,cin,ans,return,include,mod
From: https://www.cnblogs.com/cjcf/p/18434015

相关文章

  • P8563 Magenta Potion 题解
    前排警告这是较为通用,不需要脑子,但是代码量巨大的题解,请谨慎食用解题思路不知道大家做没做过带修改的区间最大连续子段和,这一题其实就是带修改的区间最大连续子段积。那么其实做法是类似的。我们用线段树维护五个量:当前区间答案,区间前缀最小值,区间前缀最大值,区间后缀最小值,区......
  • P8564 ρars/ey 题解
    显然树上背包。首先一眼会想到的状态:\(dp_{i,j}\)表示\(i\)的子树最后剩下\(j\)个结点的最小代价。然而开始写会发现这并不好DP。于是我们换一个想法:\(dp_{i,j}\)表示\(i\)的子树删去\(j\)个结点的最小代价。则有转移方程:\[dp_{i,j}=\min_{v\inson(i)}\{dp_{i......
  • 题解:UVA1456 Cellular Network
    UVA1456CellularNetwork题解夭寿了!30行写完紫题了!更新:已联系管理员修改难度,现在是绿题题意很简单,不再赘述。首先一个小贪心,将概率\(u​\)进行从大到小的排序,优先查看概率大的区域,显然这样能够保证访问数量期望最小。接着考虑如何将区域分组。一个显而易见的思路是动态......
  • 题解:CF437B The Child and Set
    CF437BTheChildandSet题解这题目就一个问题。啥是\(\operatorname{lowbit}\)?\(\operatorname{lowbit}(x)\)是指\(x\)的二进制表示中最低位的\(1\)所表示的值。例如\((14)_{10}=(1110)_2\),其中最低位的\(1\)在第二位,表示\((2)_{10}\),即\(\operatorname{lo......
  • 题解:P4288 [SHOI2014] 信号增幅仪
    很好一题目,使我的最小圆覆盖旋转。先假设\(p=1\)。这是最简单的情况。这个时候我们就得到了一个裸的最小圆覆盖。当\(p\not=1\),但是\(a=0\)的时候。圆就变成了对称轴与坐标轴平行的椭圆,运用高中知识仿射一下,又回到了最小圆覆盖。在一般的情况下,我们先通过坐标的旋转......
  • 《超级机器人大战30》缺少roboex32.dll启动遇阻怎么办?轻松应对《超级机器人大战30》ro
    当《超级机器人大战30》因缺少roboex32.dll文件而启动遇阻时,可以尝试以下几种解决方案来轻松应对这一问题:一、下载并替换缺失的DLL文件寻找可靠来源:首先,需要在互联网上找到可靠的roboex32.dll文件下载源。建议访问官方网站、微软官方下载中心或知名的软件下载站点,以确保下载......
  • AT_arc176_e [ARC176E] Max Vector 题解
    发现数据范围很小,考虑最小割。先对题面做一个转化:构造两个序列\(X=(X_1,X_2,\dots,X_N),Y=(Y_1,Y_2,\dots,Y_N)\)最小化\(\sumX_i+Y_i\),有\(M\)个限制,每个限制有一个序列\(A_1,A_2,\dots,A_n\),需要满足\(\foralli,X_i\geA_i\)或者\(\foralli,Y_i\geA_i\)。考虑怎......
  • 1. 两数之和题解
    题目描述[!NOTE]总结:找出整数数组中两数之后等于目标值的两个数,然后返回其下标给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素......
  • 「FJWC2020Day5-zzq」rng 题解
    题意简述一个长度为\(n\)的实数序列\(a_i\),其中\(a_i\)为\([l_i,r_i]\)中独立均匀随机选取的实数。你只能通过交换相邻两个数,使得\(a_i\)单调不降。你需要求出你最少操作次数的期望,对\(M=998244353\)取模。\(1\leqn\leq10^6\),\(0\leql_i\ltr_i\leq10^{1......
  • 留学期间学业常见问题解决办法,包括不能毕业的状况
    留学期间学业常见问题解决办法,包括不能毕业的状况【国外留学期间,遇到考试挂科的情况,影响了毕业,该怎么办?】考试挂科是一个很常见的现象,而国外院校,因为每个学校的规定不同,有的学校学生有补考机会,但是有的学校如果学生考试挂科情况很严重,或许就没有补考的机会了。这都没关系,重要的是,你......