首页 > 其他分享 >Codeforces Round 909 (Div. 3)

Codeforces Round 909 (Div. 3)

时间:2023-11-19 21:24:10浏览次数:47  
标签:int 909 Codeforces long cin ans Div include

Codeforces Round 909 (Div. 3)

基本情况

  • 第一次在 CFAC 了超过一道题。(毕竟是Div3
  • B 题卡住了很久。
  • D 没有深入思考。

[B. 250 Thousand Tons of TNT](Problem - B - Codeforces)

一开始死活过不了的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define ll long long

using namespace std;

const int N = 150000 + 10;
long long s[N];
long long a[N];
long long ans = 0;
int n, t;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin >> t;
	while (t--)
	{
		cin >> n;
		ans = 0;
		long long maxx = 0, minn = 0x7fffffff;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			maxx = max(a[i], maxx);
			minn = min(a[i], minn);
		}
		if (n == 1)
		{
			cout << 0 << endl;
			continue;
		}
		ans = max(ans, maxx - minn);
		for (int t = 2; t < n; t++)
		{
			if (n % t == 0)
			{
				maxx = 0, minn = 0x7fffffff;
				for (int i = t; i <= n; i += t)
				{
					ll temp = 0;
					for (int k = i - t + 1; k <= i; k++)
					{
						temp += a[k];
					}
					maxx = max(maxx, temp);
					minn = min(minn, temp);
				}
				ans = max(ans, maxx - minn);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

已经觉察到可能爆int 而改了 long long

但一是因为基础不扎实,二是不会造数据

导致我搞了快一个小时都没有发现问题所在:

minn = 0x7fffffff

我初始化的极大值只是int下的极大值,导致了数据范围超过 int 后就会出现正确性问题。

改成 minn = 0x7fffffffffffffff即可。

但凡我能直接意识到这个问题,或者手造数据时候能造大一点都能 hack 掉自己的代码。

[D. Yarik and Musical Notes](Problem - D - Codeforces)

这题我已经推到 \(2^{a - b} = \frac a b\),然而没有继续推导下去,而是尝试用不严谨的写法来做。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 2e5 + 10;
int a[N];

bool check(int n, int m)
{
	if (n == m)
		return true;
	if ((m / n) % 2)
		return false;
	return ((m / n) == (1 << (m - n)));
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int t, n;
	cin >> t;
	while (t--)
	{
		long long ans = 0;
		cin >> n;
		for (int i = 1;  i <= n; i++)
		{
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);
		for (int i = n; i >= 1; i--)
		{
			for (int j = i - 1; j >= 1; j--)
			{
				if (check(a[j], a[i]))
				{
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

显然,1 << (m - n)会爆范围。。。

看了题解,发现继续推下去就轻松了。

\(suppose\space that\space b = a \cdot 2^k(k > 0)\)

\[\frac{a}{a \cdot 2^k} = \frac{2^a}{2^{a \cdot 2^k}} \Leftrightarrow \frac{1}{2^k} = \frac{1}{2^{(2^k - 1)a}} \Leftrightarrow 2^k = 2^{(2^k - 1)a} \]

  • \(k = 1\)
    • \(a = 1, b = 2\)
  • \(k > 1\)
    • \(2^k - 1 > k\)
    • can't be satisfied

Thus, the only possible cases where the equality is satisfied are if \(b_i = b_j\) or \(b_i = 1, b_j = 2\),(and vice versa). The number of such pairs can be counted for \(\operatorname O(n \log n)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 
void solve() {
	int n;
	cin >> n;
	vector<int> a(n);
	for (int& x : a) cin >> x;
	ll ans = 0;
	map<int, int> cnt;
	for (int i = 0; i < n; i++) {
		ans += cnt[a[i]];
		if (a[i] == 1) ans += cnt[2];
		else if (a[i] == 2) ans += cnt[1];
		cnt[a[i]]++;
	}
	cout << ans << "\n";
}
 
signed main() {
	int t;
	cin >> t;
	while (t--) solve();
}

标签:int,909,Codeforces,long,cin,ans,Div,include
From: https://www.cnblogs.com/kdlyh/p/17842675.html

相关文章

  • CF909 div3
    CF909div3A.GamewithIntegers题意两人博弈,给出一个数字,每人每次可以选择令该数字+1或者-1。如果在10步以内可以令数字为3的倍数,先手胜。否则后手胜。数据范围多组数据,\(1<=T<=100,1<=n<=1000\)题解后手可以恢复现场,所以先手最多只能有效操作1次。若+1或者-1......
  • Educational Codeforces Round 13 E
    tilian最开始看错了以为是可以任意选择两人or选择一人胜出但题意是可以选择下一个擂主是谁考虑dp的话我们显然需要记录一个state以及当前擂主是谁转移就是dp[state][i]=max(dp[state][i],dp[state(1<<j)][j]*a[i][j]+dp[state(1<<i)]*a[j][i])意义是我们枚举他后一个交......
  • Codeforces Round 909 (Div. 3) A-E
    CodeforcesRound909(Div.3)A.GamewithIntegers题意:两人轮流操作,可以加一或减一,若结果能被3整除则输出First,否则输出Second思路:若n不能被3整除,则第一个人可以直接通过加一或减一使结果被3整除,反之则一定不能代码:#include<bits/stdc++.h>usingnamespacestd;cons......
  • cf1864C. Divisor Chain
    https://codeforces.com/contest/1864/problem/C思维越来越僵化了假如\(n=2^k\),直接每次/2就行。否则,我们可以考虑如何转化成上面的情况令\(n=2^kx\),那么我们显然可以转移到\(n=2^k(x-1)\),因为x是奇数,所以2的次幂会加一,最后变成\(2^k\)次方的时候,每个数最多出现两次,正好符合......
  • 一个可以拖拽缩放的div?
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metahttp-equiv="X-UA-Compatible"content="IE=edge"/><metaname="viewport"content="width=de......
  • C++ signal(SIGFPE,handler) ignore division by 0 exception
    #include<stdexcept>#include<chrono>#include<csetjmp>#include<ctime>#include<fstream>#include<iostream>#include<iomanip>#include<signal.h>#include<sstream>#include<thread>#incl......
  • 【LGR-166-Div.4】洛谷入门赛17
    【LGR-166-Div.4】洛谷入门赛#17比赛地址这次是div4的难度,整体不算是很难,很适合小白玩家[10月入门赛-A]食堂题目描述为了给师生提供良好的用餐体验,洛谷小学的食堂坚持现炒、现做每一道菜肴。洛谷小学一共有\(a\)名老师和\(b\)名学生。食堂的营养师为每位师生的用餐进......
  • CodeForces 1895E Infinite Card Game
    洛谷传送门CF传送门容易转化成经典的有向图博弈模型。每张牌建一个点,若\(x\)能打败\(y\)就连一条\(x\toy\)的边。入度为\(0\)的点为必败态,之后类似拓扑排序倒推即可。具体就是若存在边\(u\tov\),若\(u\)为必败态则\(v\)为必胜态并加入队列;若\(v\)的所有前驱......
  • CF906 div2
    CF906div2A.Doremy'sPaint3题意给出一个序列,可以随意打乱顺序,问最后能否使得所有相邻两个元素的和相等。数据范围多组数据,\(2<=n<=100,1<=a_i<=10^5\)样例输入52893112411455233334100000100000100000100000样例输出YesYesNoNo......
  • Codeforces Round 907 (Div. 2)
    \(A.SortingwithTwos\)https://codeforces.com/contest/1891/submission/232689614\(B.DejaVu\)https://codeforces.com/contest/1891/submission/232690141\(C.SmiloandMonsters\)https://codeforces.com/contest/1891/submission/232691988\(D.Suspic......