codeforce 827div4
第一次在codeforce上打题,补一下题记录成长
D题
分析:求数组中两个互质的数的最大下标和;
思路:观察到数据范围n是2e5暴力做n^2会超时,再观察数据a[i]最大为1000,所以这2e5个数中必然有很多重复的,对于重复的我们只需要保存下表最大的a[i]即可这样我们便可以将数据范围缩小到1000,直接打暴力n ^2也不会超时
#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 1010;
unordered_map<int, int>h;
struct wy{
int a;int p;
}wy[N];
inline int gcd(int a,int b)
{
return b>0 ? gcd(b,a%b):a;
}
int main() {
int m;cin >> m;
while(m --)
{
int n;cin >>n;
int cnt = 0;
int ans = 0;
for(int i = 1; i <= n; i ++)
{
int k; cin >> k;
if( h.find(k) == h.end())
{
wy[++ cnt] = {k, i};
h[k] = cnt;
}
else{
wy[h[k]] = {k,i};
}
}
bool st = false;
for(int i = 1; i <= cnt; i ++)
for(int j = 1; j <= cnt; j ++)
{
if(gcd(wy[i].a, wy[j].a) == 1)
{
ans = max(ans, wy[i].p + wy[j].p);
st = true;
}
}
if(st) cout << ans << endl;
else cout << -1 << endl;
h.clear();
}
return 0;
}
E题
分析:只需要在数组中找到第一个比k大的数即可,数据范围2e5所以不能暴力循环,观察题中的特点我们可以在原数组中构造一个单调递增的序列,如果k>a[i],那么原序列中a[i] ~ a[i + 1] 的数一定< a[i]也是满足条件的,这样我们求的是一段连续序列的和,可以用前缀和优化,这样便可以优化到nlogn
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
long long a[N],s[N];
int main() {
int m; cin >> m ;
while(m --)
{
int la, lb; cin >> la >> lb;
for(int i = 1; i <= la; i ++)
{
long long k; cin >> k;
a[i] = max(a[i - 1], k);
s[i] = s[i - 1] + k;
}
while(lb --)
{
int q; cin >> q;
cout << s[upper_bound(a + 1, a + la + 1, q) - a - 1] << " ";
}
cout << endl;
}
return 0;
}
F题
分析:给定两个初始均为a的字符串,可以进行两种操作,一种是给第一个串加上k个str串,第二种是给第二个串加上k个str串,进行操作之后可以对两个串进行重新排序,问是否能有一种方案使串2的字典序大于串1
我们讨论串2进行k次操作后最大的字符,如果最大的字符>a那么只需把串1的a放在第一位即可,如果串2的最大字符就是a,只有使串1的最大字符也是a并且串1的长度小于串2的长度,所以每次操作后需要记录两个串的长度以及当前的最大字符即可
最重要的要观察一下数的范围会爆int
#include <iostream>
typedef long long ll;
using namespace std;
int main() {
int m; cin >> m;
while(m --) {
int k;
cin >> k;
char mxa = 'a', mxb = 'a';
ll la = 1, lb = 1;
while (k--)
{
int op, q;
string str = "";
cin >> op >> q >> str;
int ls = str.size();
if (op == 1)
{
for (int i = 0; i < str.size(); i++) mxa = max(mxa, str[i]);
la += 1ll *ls * q;
}
else
{
for (int i = 0; i < str.size(); i++) mxb = max(mxb, str[i]);
lb += 1ll *ls *q;
}
if(mxb > 'a'|| ((mxa == 'a') && lb > la)) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}
G题
分析:
1、首先或运算只会产生两种结果要么使一个数增大要么不变
2、每次我们都确定一个数使前面已经或起来的数和我们在剩余所有数中挑选出来的这个数或起来的结果是最大的,即可。
3、考虑要挑选多少次,题中所给的数是1e9,我们考虑最坏的情况每次或都会使当前数增加,那么1e9 < 2^31,在31次挑选数后即使我们随意放数也不会使这个数再增加了。
#include <iostream>
using namespace std;
const int N = 2e5 + 10;
typedef long long LL;
inline void solve()
{
LL a[N], st[N];
int n; cin >> n;
int cur = 0;
for(int i = 0; i < n; i ++)
{
scanf("%d", &a[i]);
st[i] = false;
}
for(int i = 0; i < min(31, n); i ++)
{
int mx = 0, idx = -1;
for(int j = 0; j < n; j ++)
{
if(st[j]) continue;
if(mx < (cur | a[j]))
{
mx = (cur | a[j]);
idx = j;
}
}
st[idx] = true;
cout << a[idx] << " ";
cur = (cur | a[idx]);
}
for(int i = 0; i < n; i ++) if(!st[i]) cout << a[i] << " ";
cout << endl;
}
int main() {
int m; cin >> m;
while(m --) solve();
return 0;
}
codeforce 827div4的更多相关文章
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- Codeforce 水题报告(2)
又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...
- codeforce 375_2_b_c
codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...
- codeforce 367dev2_c dp
codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...
- 三维dp&codeforce 369_2_C
三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...
- 强连通分量&hdu_1269&Codeforce 369D
强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...
- 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D
[树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...
- 解题报告:codeforce 7C Line
codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...
- Two progressions CodeForce 125D 思维题
An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...
随机推荐
- python笔记:第十二章文件
1.打开文件 位于自动导入的模块IO中,无需手动导入. f = open('D:\M\test.txt') 若文件不存在,则报错 Traceback (most recent call last): ...
- Centos7安装Python3.x
一.修改yum源 查看Centos发行版本 cat /etc/redhat-release 换阿里云yum源 备份原始yum源 mv /etc/yum.repos.d/CentOS-Base.repo ...
- hadoop 启动增加DEBUG信息
export HADOOP_ROOT_LOGGER=DEBUG,console
- 最全面的JAVA多线程知识总结
背景: 2023年经营惨淡,经历了裁员就业跳槽再就业,在找工作过程中对于知识的梳理和总结,本文总结JAVA多线程. 应用场景: 需要同时执行多个任务或处理大量并发请求时, 目前常用的场景有: We ...
- python:map函数
参考示例 def test(x): return x * 2 mylist = [1, 2, 3, 4, 5] result = list(map(test, mylist)) print(resul ...
- Mysql高级5-SQL优化
一.插入数据优化 1.1 批量插入 如果有多条数据需要同时插入,不要每次插入一条,然后分多次插入,因为每执行一次插入的操作,都要进行数据库的连接,多个操作就会连接多次,而一次批量操作只需要连接1次 1 ...
- RocketMq消费原理及源码解析
消费原理概览 先简单说下常见的rocketMq的部署方式,上图中broker为真正计算和存储消息的地方,而nameServer负责维护broker地 图中右侧consume message部分即是本文 ...
- [Go笔记] 基础-01: Golang发展简史、著名项目及基本使用
引言 Golang,又称Go语言,是一门开源的静态类型编译型编程语言.自从2007年由谷歌的罗伯特·格里泽默(Robert Griesemer).罗布·派克(Rob Pike)和肯·汤普森(Ken T ...
- Fork me on GitHub彩带添加方法
在博客添加GitHub彩带的方法 针对博客园博客追加如图彩带的方法 依次进入 管理 → 设置 → 页首Html代码 将如下代码粘贴在该处 <a target="_blank" ...
- call与retn指令
一. call指令 将call指令下一跳指令压入栈中 jmp跳转到call指令的地址 二. retn指令 pop指令将栈顶元素弹出存储 jmp跳转到该栈顶元素地址 retn n;表示再前两步操作的基础 ...