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个折扣的时候,你必须 ...
随机推荐
- Mysql安装维护笔记一
1.Centos7安装mysql rpm安装PHP7相应的yum源 $wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11. ...
- java_05_IO
java_05_IO 1,动手动脑 使用Files. walkFileTree()找出指定文件夹下所有大于指定大小(比如1M)的文件. 分析思路: 1)找到该文件夹下所有文件. 2)找出其中字节数大于 ...
- schema 文件约束
1. 在javaproject 中创建一个.xsd 文件 <?xml version="1.0" encoding="UTF-8" ?> <! ...
- 剑指offer_12.31_Day_1
不用加减乘除做加法 题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 不用四则运算,必然是依靠位运算. 位运算包括,与,或,异或,取反,左移,右移. 分别为 ...
- (简单模拟)P1540 机器翻译
题解: #include<iostream>#include<cmath>using namespace std; int main(){ int m,n; cin>&g ...
- promise核心技术 2.两种回调函数 js中error的处理
抽空详细学习一下什么是回调函数(一个回调函数,也被称为高阶函数) 1.什么样的函数是回调函数 自己定义的(sittimeout不是自己定义的) 没有调用 自己执行 1.同步回调与异步回调函数 同步回调 ...
- unix中嘚vim编辑器
在linux家族中,vim编辑器是系统自带的文本编辑器,其功能强大自不必说了. 偶有小白,刚接触linux,要修改某个文本文件,不可能像WINDOWS那样操作,更有甚者,进入VI编辑器后,无法退出以致 ...
- java8中的map 和reduce
map 1.使用map让集合里面的数字翻倍. List<Integer> numbers = Lists.newArrayList(1,2,3,4,5);List<Integer&g ...
- 74)搭建TCP服务器
补充: 1-->listen是监听,就是监听你建立的那个socket那个窗口,要是有客户端来了,那么就把他加到 队列里面,然后accept是从队列中取客户端(就是把对应的客人的信息拿过来,交给w ...
- 学习SEO之7天精通SEO
这本书大致看了一下,对于SEO基本上有了一个初步的认识,附上链接以供学习之用. 百度网盘:https://pan.baidu.com/s/1Bntzh2YF4tBd2AYAL1Q8vQ 心得:1.SE ...