Codeforces Round #624 (Div. 3)(题解)
A. Add Odd or Subtract Even
思路:
相同直接为0,如果两数相差为偶数就为2,奇数就为1
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int kk;
scanf("%d",&kk);
while(kk--){
int n,m;
cin>>n>>m;
if(n==m) {printf("0\n");continue;}
if(m-n>){
if((m-n)%){cout<<<<endl;continue;}
else {cout<<<<endl;continue;}
}
if(!((n-m)%)) {
cout<<<<endl; continue;}
else cout<<<<endl;
}
}
B. WeirdSort
思路:
记录哪些位置可以交换,然后不断循环遍历数组直到没有交换发生,最后再判断一下是否符合要求即可,最坏时间复杂度为冒泡排序O(N2)
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=;
int a[maxn],b[maxn],flag[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(flag,,sizeof(flag));
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=m;i++) scanf("%d",&b[i]),flag[b[i]]=;
int num=;
while(num){
num=;
for(int i=;i<=n;i++){
if(flag[i]){
if(a[i]>a[i+]){
num=;
swap(a[i],a[i+]);
}
}
}
}
int kk=;
for(int i=;i<n;i++)
if(a[i]>a[i+]) kk=;
if(kk) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return ;
}
C. Perform the Combo
思路:
根据会按错的位置,我们开个数组mp[i] 记录下犯错位置i的次数,然后一个数组c 来维护前缀和按错的字母的总和 然后每次碰到mp[i] 有值的话,我们去循环26个字母,把答案数组a加上当前维护的前缀的c*mp[i] 也就是当前位置犯错的次数就好了
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll mp[];
int b[];
char s[];
int main()
{
int t;
cin>>t;
while(t--)
{
ll a[]={};
ll c[]={};
int n,m;cin>>n>>m;
cin>>s+;
for(int i=;i<=n;i++) mp[i]=;
for(int i=;i<=m;i++){
cin>>b[i];
mp[b[i]]++;
}
for(int i=;i<=n;i++){
a[s[i]]++;
c[s[i]]++;
if(mp[i]){
for(int j='a';j<='z';j++){
a[j]=a[j]+(mp[i]*c[j]);
}
}
}
for(int i='a';i<='z';i++) cout<<a[i]<<" ";
puts(""); }
return ;
}
D. Three Integers
思路:
枚举i,然后枚举i的倍数j,再枚举j的倍数k,找到最小值即可,注意枚举范围一定要大
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
int t,a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&a,&b,&c);
int ans=inf,x=,y=,z=;
for(int i=;i<=;i++){
for(int j=i;j<=;j+=i){
for(int k=j;k<=;k+=j){
int kk=abs(a-i)+abs(b-j)+abs(c-k);
if(kk<ans){
ans=kk,x=i,y=j,z=k;
}
}
}
}
cout<<ans<<endl;
cout<<x<<" "<<y<<" "<<z<<" "<<endl;
}
return ;
}
F. Moving Points(树状数组+离散化)
思路:
如果Posx < Posy &&Vx < Vy 那么两点的距离最小值就会为0,否则都为坐标的差值,那答案就是统计∑dis(i,j) (Posi < Posj && Vi ≤ Vj)
看到位置跟速度的范围很大,首先将二者离散化,然后先按照坐标排序
一个点对答案的贡献就为所有坐标比他小并且速度比他小的点距离差值的和,所以就成了一个偏序问题
开两个树状数组sum[0][x]记录速度小于x的点的个数,sum[1][x]记录速度小于x的点的坐标和 动态加点就好了
#include<iostream>
#include<algorithm>
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long ll;
const int maxn=2e5+;
struct node{
int x,v;
}a[maxn];
int speed[maxn],n;
ll sum[][maxn];
int cmp(node a,node b){return a.x<b.x;}
void add(int x,int val)
{
while(x<=n){
sum[][x]++,sum[][x]+=val;
x+=lowbit(x);
}
}
ll query(int x,int k)
{
ll ans=;
while(x>=){
ans+=sum[k][x];
x-=lowbit(x);
}
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i].x);
for(int i=;i<=n;i++) scanf("%d",&a[i].v),speed[i]=a[i].v;
sort(a+,a++n,cmp);
sort(speed+,speed++n);
unique(speed+,speed++n);
ll ans=;
for(int i=;i<=n;i++){
int now=lower_bound(speed+,speed++n,a[i].v)-speed;
ans+=a[i].x*query(now,)-query(now,);
add(now,a[i].x);
}
cout<<ans<<endl;
return ;
}
Codeforces Round #624 (Div. 3)(题解)的更多相关文章
- Codeforces Round #624 (Div. 3)(题解)
Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
随机推荐
- spring教程
Spring框架是Java EE开发中最流行的框架,已经成为JEE事实上的标准,全世界的开发人员都在使用Spring框架开发各种应用.随着Spring boot,Spring cloud新版本的不断推 ...
- 【剑指Offer】面试题05.替换空格
题目 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 示例 1: 输入:s = "We are happy." 输出:"We%20are ...
- 面试题(7)之 leetcode-003
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...
- SQL中的LEFT/RIGHT/SUBSTRING函数
语法: LEFT(field,length) RIGHT(field,length)SUBSTRING(field,start,length) LEFT/RIGHT函数返回field最左边/最右边的l ...
- 如何通过C语言获取主机WLAN下的IPv4地址,MAC地址
#include "stdio.h" #include "windows.h" void GetHostWLAN_IPv4_AND_MAC(char IPv4[ ...
- SASS - @extend(继承)指令
SASS – 简介 SASS – 环境搭建 SASS – 使用Sass程序 SASS – 语法 SASS – 变量 SASS- 局部文件(Partial) SASS – 混合(Mixin) SASS ...
- c#textBox控件限制只允许输入数字及小数点,是否为空
c#textBox控件限制只允许输入数字及小数点 转载 //判断按键是不是要输入的类型. if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) ...
- @EnableAutoConfiguration激活自动装配
给予上个例子,将WebConfiguration类上的@SpringBootApplication换成@EnableAutoConfiguration.启动并运行http://localhost:80 ...
- 项目进度02-Day3
①今天做了什么? 数据库数据的重置.之前的用户类字段的补充.简单的平台信息查询 ②明天要做什么? 分类浏览和综合查询功能. ③遇到了什么问题? 出现问题:Parameter index out o ...
- JAVA基础——使用配置文件
一. 前言 日常我们做项目中,我们经常会遇到这样的情况:由于开发环境和生产环境的不同,项目部署在生产环境之前,有些参数我们并不知道如何取值.例如:数据库链接设定,我们在部署生产环境之前 ...