[Codeforces Round #841]

Codeforces Round #841 (Div. 2) and Divide by Zero 2022

A. Joey Takes Money

Joey Takes Mone

Problem:

给一个正整数序列 \(a_1,a_2,…,a_n (n≥2)\) ,能进行任意次操作,操作是:找到 \(x\) 和 \(y\) 使得 \(x⋅y=a_i⋅a_j\) ,分别用 \(x\) 和 \(y\) 替换 \(a_i\) 和 \(a_j\) ,求操作过后最大的序列和

Solution:

贪心,对于两个正整数:

\(a*b + 1-(a+b) = (a-1)(b-1) \ge 0\) 恒成立

所以将两数相乘并取 \(a*b\) 和 \(1\) 一定会使贡献增加

而两个乘的数越大,越能使贡献增加

所以贪心策略是把最大的数和其他所有的数乘,最后别忘了加上\(n-1\) 个 \(1\)

Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int read()
{
int x = 0,f = 1;char ch = getchar();
while(ch < '0'||ch > '9'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0'&&ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
return x*f;
}
int a[60];
signed main()
{
int t = read();
while(t--)
{
int n = read();
for(int i = 1;i <= n;i++) a[i] = read();
sort(a+1,a+n+1);
int ans = a[n];
for(int i = n - 1;i >= 1;i--)
{
ans*=a[i];
}
ans+=n-1;
cout<<ans*2022<<endl;
}
return 0;
}

B. Kill Demodogs

Kill Demodogs

Problem:

给一个 \(n \times n\) 的方格 ,第 \(i\) 行第 \(j\) 列的数值是 \(x⋅y\)

从 \((1,1)\) 走到 \((n,n)\) ,只能向下或向右走,求操作过后最大的序列和

Solution:

首先,可以看出这个方格是一个对称矩阵

其次,通过目测可以看出一直贴着主对角线走贡献最大

答案就是$ ∑i^2 + ∑i(i-1)$

$ ∑i^2 = n(n+1)(2n+1)/6$

\(∑i(i-1) = ∑i^2 -∑i = n(n+1)(2n+1)/6 -n(n+1)/2\)

把二者加起来,\(O(1)\) 输出

Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9+7;
int read()
{
int x = 0,f = 1;char ch = getchar();
while(ch < '0'||ch > '9'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0'&&ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
return x*f;
}
signed main()
{
int t = read();
while(t--)
{
int n = read();
int ans = ((337*n%mod)*(n+1)%mod)*(4*n%mod-1+mod)%mod;
cout<<ans%mod<<endl;
}
return 0;
}

C. Even Subarrays

Even Subarrays

Problem:

给一个正整数序列 \(a_1,a_2,…,a_n (1≤a_i≤n)\) 找到 \((i,j)\) 使得 $ a_i⊕a_{i+1}+⋯⊕a_j$ 有偶数个因数

完全平方数有奇数个因数,特别的,0被认为有奇数个因数

多组数据,\(1≤t≤10^4\) , \(2≤n≤2⋅10^5\)

Solution:

首先要熟悉异或前缀和的概念,求出前缀和后

$ a_i⊕a_{i+1}+⋯⊕a_j$ 即为 $ sum_{i-1} ⊕sum_j$

枚举序列,对于当前数 \(i\) ,考虑从前面的前缀和中找到位置\(x (x < i)\) 使得 $ sum_{x} ⊕sum_i = k$ ,k不为完全平方数

那么其实就是找前面有没有异或前缀和为 \(sum_{y}⊕k\) ,考虑开一个桶记录数量

考虑时间复杂度,枚举 $ k$ 为 \(O(n^2)\) ,可以枚举完全平方数,最后用总子序列数减去,时间复杂度 \(O(n\sqrt{n})\)

注意细节:前缀和为0的数量初始值为1,序列中两个数异或后理论最大值是\(2n\),枚举 \(k\) 时要考虑到

Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9+7;
int read()
{
int x = 0,f = 1;char ch = getchar();
while(ch < '0'||ch > '9'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0'&&ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
return x*f;
}
int a[200010];
int sum[200010];
int cnt[1000010];
signed main()
{
int t = read();
while(t--)
{
int n = read();
for(int i = 1;i <= n;i++) a[i] = read();
int ans = 0;
cnt[0] = 1;//注意
for(int j = 1;j <= n;j++)
{
sum[j] = sum[j-1]^a[j];
for(int i = 0;i*i<=2*n;i++)//2*n注意
{
ans += cnt[sum[j]^(i*i)];
}
cnt[sum[j]]++;
}
cout<<(n+1)*n/2-ans<<endl;
for(int i = 1;i <= n;i++) cnt[sum[i]] = 0,sum[i] = 0,a[i] = 0;
}
return 0;
}

D. Valiant's New Map

Valiant's New Map

Problem:

给一个 \(n \times m\) 的方格,每个方格上有数\(a_{i,j}\)

找到一个数 \(l\) 使得 方格中存在一个 \(l \times l\) 的正方形,其中所有的数都大于等于\(l\),求\(l\)最大值

多组数据,\(1≤t≤1000\) , \(1≤n⋅m≤10^6\)

Solution:

比较裸的二分题:

主要有两个细节

一是给的范围是 \(1≤n⋅m≤10^6\) 存数要二维转一维

二是怎么较好的判断,这里的方法是运用二维前缀和,把\(a_{i,j} \ge l\)的赋值为1,否则为0,直接判断正方形中数据和是否为 \(l \times l\)

Code:

#include <bits/stdc++.h>
using namespace std;
int read()
{
int x = 0,f = 1;char ch = getchar();
while(ch < '0'||ch > '9'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0'&&ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
return x*f;
}
int dp[1000010],a[1000010];
int n,m;
int tr(int x,int y)
{
return (x-1)*m+y;
}
bool check(int k)
{
for(int i = 1;i <= n*m;i++) dp[i] = a[i] >= k?1:0;
for(int i = 1;i <= n*m;i++)
{
int x = (i-1)/m + 1,y = (i-1)%m + 1;
dp[i] = dp[i] + dp[tr(x-1,y)] + dp[tr(x,y-1)] - dp[tr(x-1,y-1)];
}
for(int i = tr(k,k);i <= n*m;i++)
{
int x = (i-1)/m + 1,y = (i-1)%m + 1;
if(x < k||y < k) continue;
int ans = dp[i] - dp[tr(x-k,y)] - dp[tr(x,y-k)] + dp[tr(x-k,y-k)];
if(ans == k*k) return true;
else continue;
}
return false;
}
int main()
{
int T = read();
while(T--)
{
n = read(),m = read();
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
a[tr(i,j)] = read();
}
}
int l = 1,r = min(n,m);
int ans = l;
while(l <= r)
{
int mid = l + r>>1;
if(check(mid))
{
ans = mid;
l = mid+1;
}
else r = mid-1;
}
cout<<ans<<endl;
}
return 0;
}

E\F 没来得及看233

[Codeforces Round #841]的更多相关文章

  1. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #370 - #379 (Div. 2)

    题意: 思路: Codeforces Round #370(Solved: 4 out of 5) A - Memory and Crow 题意:有一个序列,然后对每一个进行ai = bi - bi  ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  10. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. P1084 [NOIP2012 提高组] 疫情控制 (二分答案、贪心)

    因为若一个时间限制满足题意,则所有比它大的时间限制一定都满足题意,因此本题答案具有单调性,可以想到二分答案求解. 本题思路不是很难,但细节和代码实现比较复杂. 见牛人博客:https://www.lu ...

  2. 洛谷P4011 【网络流24题】 孤岛营救问题 (BFS+状压)

    一道妙题啊......(不知道为什么这道题的标签是网络流,不需要用网络流啊) 如果没有门和钥匙,连边(边权为1)求最短路就行了. 但是有这两个因素的限制,我们采用分层建图的思想,一共2p层,每层对应持 ...

  3. Future详解

    Future模式 [1]Future模式是多线程开发中常见的设计模式,它的核心思想是异步调用.对于Future模式来说,它无法立即返回你需要的数据,但是它会返回一个契约,将来你可以凭借这个契约去获取你 ...

  4. springboot+bootstrap实现图书商城管理(大三下学期课程设计)

    在csdn上记一次自己的课程设计过程(已经实习两个月了.感觉这个很容易做.支付可能需要多花点时间.): 在此框架基础之上权限认证管理设置成功:https://blog.csdn.net/weixin_ ...

  5. 【ps下载与安装】Adobe Photoshop 2022 for Mac v23.5 中文永久版下载 Ps图像编辑软件

    Adobe Photoshop 2022 mac破解版,是一款Ps图像编辑软件,同时支持M1/M2芯片和Intel芯片安装,此主要的更新包括多个新增和改进的功能,例如改进的对象选择工具,其悬停功能可预 ...

  6. 折腾黑苹果-小新Pro13

    最近在闲鱼上购入了一台2020版的联想小新 Pro13,i5 10200u 16g 512g配置,Ax201网卡.这台机子原生硬件就可以完美黑苹果了,不需要更换配件.只是Ax201网卡不能随航和隔空投 ...

  7. 题解 SP10500 HAYBALE - Haybale stacking

    前言 想了好久树状数组啥的,后来想想写打个差分再说,结果写完一遍AC了-- 强烈安利 题意 一个由 \(n\) 个元素组成的序列,给出 \(k\) 个操作,每次将 \(a\sim b\) 加上 \(1 ...

  8. numpy常用知识点备忘(2)

    x.ravel() 和 x.flatten() : 将多为数组降维到1维.ravel()返回元素的引用(对象不一样,但是元素是引用),flatten()返回新的元素. np.meshgrid(x, y ...

  9. docker常用配置以及命令

    1. Docker基本概念 1.1 什么是 docker hub DockHub是一个仓库 https://hub.docker.com/ 仓库是集中存放镜像文件的场所 仓库分为公开仓库(Public ...

  10. jupyter notebook使用相对路径的方法

    在当前文件夹路径下开启jupyter notebook 这样就可以直接使用相对路径了,而不用管绝对路径这一令人心烦的问题 首先需要重新安装PowerShell 下载链接:https://cloud.1 ...