首页 > 其他分享 >CF Round946 (Div. 3)C:map的map<pair<int,int>int>映射 + 性质

CF Round946 (Div. 3)C:map的map<pair<int,int>int>映射 + 性质

时间:2024-05-23 22:08:08浏览次数:13  
标签:map pairs le triples int Round946 CF triple test

Beautiful Triple Pairs

题目描述

Polycarp was given an array $ a $ of $ n $ integers. He really likes triples of numbers, so for each $ j $ ( $ 1 \le j \le n - 2 $ ) he wrote down a triple of elements $ [a_j, a_{j + 1}, a_{j + 2}] $ .

Polycarp considers a pair of triples $ b $ and $ c $ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied:

  • $ b_1 \ne c_1 $ and $ b_2 = c_2 $ and $ b_3 = c_3 $ ;
  • $ b_1 = c_1 $ and $ b_2 \ne c_2 $ and $ b_3 = c_3 $ ;
  • $ b_1 = c_1 $ and $ b_2 = c_2 $ and $ b_3 \ne c_3 $ .

Find the number of beautiful pairs of triples among the written triples $ [a_j, a_{j + 1}, a_{j + 2}] $ .

输入格式

The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.

The first line of each test case contains a single integer $ n $ ( $ 3 \le n \le 2 \cdot 10^5 $ ) — the length of the array $ a $ .

The second line of each test case contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \le a_i \le 10^6 $ ) — the elements of the array.

It is guaranteed that the sum of the values of $ n $ for all test cases in the test does not exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, output a single integer — the number of beautiful pairs of triples among the pairs of the form $ [a_j, a_{j + 1}, a_{j + 2}] $ .

Note that the answer may not fit into 32-bit data types.

样例 #1

样例输入 #1

8
5
3 2 2 2 3
5
1 2 1 2 1
8
1 2 3 2 2 3 4 2
4
2 1 1 1
8
2 1 1 2 1 1 1 1
7
2 1 1 1 1 1 1
6
2 1 1 1 1 1
5
2 1 1 1 1

样例输出 #1

2
0
3
1
8
4
3
2

提示

In the first example, $ a = [3, 2, 2, 2, 3] $ , Polycarp will write the following triples:

  1. $ [3, 2, 2] $ ;
  2. $ [2, 2, 2] $ ;
  3. $ [2, 2, 3] $ .

The beautiful pairs are triple $ 1 $ with triple $ 2 $ and triple $ 2 $ with triple $ 3 $ .In the third example, $ a = [1, 2, 3, 2, 2, 3, 4, 2] $ , Polycarp will write the following triples:

  1. $ [1, 2, 3] $ ;
  2. $ [2, 3, 2] $ ;
  3. $ [3, 2, 2] $ ;
  4. $ [2, 2, 3] $ ;
  5. $ [2, 3, 4] $ ;
  6. $ [3, 4, 2] $ ;

The beautiful pairs are triple $ 1 $ with triple $ 4 $ , triple $ 2 $ with triple $ 5 $ , and triple $ 3 $ with triple $ 6 $ .

AC_code

要统计一位不同,即两位相同的次数,但要注意当完全一样即会被计算3次,要减去3次

#include <bits/stdc++.h>

using namespace std;

using LL = long long;

const int N = 2 * 1e5 + 10;

int a[N];
int _, n;

void solve()
{
    cin >> n;
    for(int i = 1; i <= n; ++ i) cin >> a[i];
    LL ans = 0;
    
    map<pair<int, int>, int> mp1, mp2, mp3;
    map<vector<int>, int> cnt;
    
    for(int i = 1; i + 2 <= n; ++ i) {
        ans += mp1[{a[i], a[i + 1]}] ++;
        ans += mp2[{a[i], a[i + 2]}] ++;
        ans += mp3[{a[i + 1], a[i + 2]}] ++;
        
        vector<int> v = {a[i], a[i + 1], a[i + 2]};
        ans -= cnt[v] * 3;
        cnt[v] ++;
    }
    cout << ans << endl;
}

int main()
{
    cin >> _;
    while(_ -- ) {
        solve();
    }
    return 0;
}

本人思路

一眼样例感觉好像是全排列+处理冗余,但是需要额外处理每个数能用多少次(这个显然我不会),继续看样例发现是按长度为3依次枚举,转而想用最近学到的那个分段枚举,但是不同1个的串的顺序不同可能导致问题,最后直接暴力扫一遍维护长度为3的串匹配

//错误代码
int res = 0;
    int v[3] = {0};
    int r[3] = {0};
    for(int i = 1; i <= n - 2; ++ i) {
        v[0] = q[i], v[1] = q[i + 1], v[2] = q[i + 2];
        for(int j = i + 1; j < i + 4; ++ j) {
                c = 0;
                if(j + 2 > n) break;
                r[0] = q[j], r[1] = q[j + 1], r[2] = q[j + 2];
                for(int i1 = 0; i1 < 3; ++ i1)
                    for(int j1 = 0; j1 < 3; ++ j1)
                        if(v[i1] == r[j1]) ++ c;
                if(c >= 2) ++ res;
        }
    }

标签:map,pairs,le,triples,int,Round946,CF,triple,test
From: https://www.cnblogs.com/OVSolitario-io/p/18209168

相关文章

  • ccf 201409-2 画图
    http://t.csdnimg.cn/uJ2u9试题编号:201409-2试题名称:画图时间限制:1.0s内存限制:256.0MB问题描述:问题描述在一个定义了直角坐标系的纸上,画一个(x1,y1)到(x2,y2)的矩形指将横坐标范围从x1到x2,纵坐标范围从y1到y2之间的区域涂上颜色。下图给出了一个画了两个矩形的例子......
  • CCF/CSP认证-第一次-命令行选项
    1.问题1.1命令行选项请你写一个命令行分析程序,用以分析给定的命令行里包含哪些选项。每个命令行由若干个字符串组成,它们之间恰好由一个空格分隔。这些字符串中的第一个为该命令行工具的名字,由小写字母组成,你的程序不用对它进行处理。在工具名字之后可能会包含若干选项,然后......
  • 「杂题乱刷」CF1759F
    题目链接CF1759FAllPossibleDigits(luogu)CF1759FAllPossibleDigits(codeforces)题意简述有一个长度为\(n\)的\(p\)进制数,你需要求出至少通过几次操作才可以让\(0\simp-1\)这\(p\)个数字都至少出现过一遍(包括中间过程)。解题思路我们很容易就能发现答案是......
  • Kubernetes-ConfigMap详解
    简介:一、ComfigMap的创建1.使用目录创建2.使用文件创建3.使用命令行创建二、Pod中使用ConfigMap1.使用ConfigMap代替环境变量2.使用ConfigMap设置命令行参数3.使用ConfigMap用做数据卷插件三、ConfigMap的热更新简介:ConfigMap功能在Kubernetes1.2版本中引入,许......
  • Kubernetes集群中配置Ingress支持HTTPS访问(一):cfssl
    目录一.系统环境二.前言三.对称加密和非对称加密简介四.什么是HTTPS五.Ingress简介六.配置ingress对外发布服务6.1安装NGINXingresscontroller控制器6.2创建pod6.3为pod创建svc服务6.4使用ingress发布服务6.5访问服务6.5.1使用Linux客户端来访问服务6.5.2使用Windows客户......
  • 在Flink中jackson-databind包下的ObjectMapper处理大写字段问题
    需要加上配置,不然解析会失败,产生一个空对象objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true);//忽略大小写代码:publicclassStreamingJob{publicstaticvoidmain(String[]args)throwsException{finalLoggerlogger......
  • 3/24MapReduce面试必看
    本质上是三个进程运行,一个maptask一个reducetask 一个MR程序写程序 添加依赖后,mapperreducer driveryarn集群的配置为了实现数据落盘和网络传输还要进行序列化和反序列化,本质就是将各个结构体里的基本数据类型一一传递 实现writable接口顺序要一致输入和输出基本......
  • HttpURLConnection 调用soap 并且使用Dom4j解析多层级XML为Map对象
    1.引入dom4j的maven依赖包<dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.4</version></dependency>2.转map方法1importjava.io.BufferedReader;2importjava.io.InputStrea......
  • 关于vue-baidu-map的一些记录
    这一阶段主要的内容是负责编写和百度地图相关的信息。(写到我想吐)仿照导航的页面效果。1.使用说明这里使用的是vue-baidu-map相关组件,这里我就不去说明如何去安装他们了,我们直接向下看。vue-baidu-map的操作手册的网址:VueBaiduMap(dafrok.github.io)。当然我这里介绍的那些只......
  • kimi- MarkMap 生成思维导图
    1、Prompt:帮我分析《被人讨厌的勇气》这本书,从里面总结出核心内容,要求:1.提供5个主要观点2.每个观点至少有3个支撑观点说明3.按照以下格式,使用markdown的代码快格式输出:```#被人讨厌的勇气##<观点1>-<支撑观点1>-<支撑观点2>-<支撑观点3>##<观点2>-<支撑......