第一次在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的更多相关文章

  1. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  2. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  3. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  4. codeforce 375_2_b_c

    codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...

  5. codeforce 367dev2_c dp

    codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...

  6. 三维dp&codeforce 369_2_C

    三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...

  7. 强连通分量&hdu_1269&Codeforce 369D

    强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...

  8. 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D

    [树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...

  9. 解题报告:codeforce 7C Line

    codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...

  10. Two progressions CodeForce 125D 思维题

    An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...

随机推荐

  1. iOS 循环引用的问题总结

    原因: self -> Timer -> target(self), 造成循环引用 导致控制器不会销毁,不会调用dealloc 方法,内存泄漏 - (void)dealloc{ [_tim ...

  2. pip install lxml 总是失败

  3. 【转载】Linux虚拟化KVM-Qemu分析(一)

    原文信息 作者:LoyenWang 出处:https://www.cnblogs.com/LoyenWang/ 公众号:LoyenWang 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者 ...

  4. 2021-6-17 plc连接

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. pandas 缺失值与空值处理

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lwgkzl/article/detail ...

  6. pyinstaller 安装报错,环境是python3.7

    在pycharm中安装,和直接输入pip install pyinstaller 均报错, 最后,输入pip install -i https://pypi.douban.com/simple/ py ...

  7. PyQt5实时刷新

    对于执行很耗时的程序来说,由于PyQt需要等待程序执行完毕才能进行下一步,这个过程表现在界面上就是卡顿,而如果需要执行这个耗时程序时不断的刷新界面.那么就可以使用QApplication.proces ...

  8. PHPstudy+Xdebug动态调试代码过程中遇到一分钟就超时问题的解决办法

    环境是PhpStorm+Xdebug+WAMP 在实际调试的过程中 碰到了调试还没走完就自动结束的情况 很尴尬 查阅了相关文档资料 找到了解决方法 首先在php.ini中进行修改 我的配置文件地址在 ...

  9. 【分享】如何才能简洁高效不失优雅的爆破ZIP文件?

    0x01 前言 在CTF比赛中,压缩包密码的爆破一直是一个热门话题.在这个过程中,简洁高效的方法是至关重要的.本文将介绍一些实用的技巧和工具,帮助您高效地爆破ZIP文件密码,而不失优雅.我们将探讨一些 ...

  10. 1. 通俗易懂的Redis基础

    通俗易懂的Redis基础教程(基于CentOS 7) 目录 通俗易懂的Redis基础教程(基于CentOS 7) 1 Redis是什么 1.1 NoSQL概念 1.2 NoSQL与SQL比较 1.3 ...