随笔 - 9  文章 - 0 评论 - 0 阅读 - 263
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

给你一个数组 a1,a2…an 。请计算有多少个图元 (i,j,k,l)符合以下条件:
· 1 i<j<k<ln
· ai= ak 和 aj = al

Input
The first line contains a single integer t (1t100) — the number of test cases.

The first line of each test case contains a single integer n (4n3000) — the size of the array a.

The second line of each test case contains n integers a1,a2,…,an(1≤ain) — the array a.

It's guaranteed that the sum of n in one test doesn't exceed 3000.

Output
For each test case, print the number of described tuples.

题目链接:https://codeforces.com/contest/1400/problem/D

哈希一下其实可以更快,但是数据量不大,n方解决即可;

AC代码:


#include"bits/stdc++.h"
using namespace std;
using ll=long long;
#define int long long
const int Mod=998244353;


void save_the_people(){
    int n,ans{0};
    cin>>n;
    vector<int> a(n);
    for(int i{0};i<n;i++){
        cin>>a[i];
        a[i]--;
    }
    vector<int> c1(n),c2(n);
    for(int i{0};i<n;i++){
        fill(c1.begin(), c1.end(),0);
        fill(c2.begin(), c2.end(),0);
        for(int l{i+1};l<n;l++){
            c2[a[l]]++;
        }
        int res{0};
        for(int k{i+1};k<n;k++){
            res-=c1[a[k]];
            c2[a[k]]--;
            if(a[i]==a[k])
                ans+=res;
            res+=c2[a[k]];
            c1[a[k]]++;
        }
    }
    cout<<ans<<"\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t{1};
    cin>>t;
    while(t--) {
        save_the_people();
    }

}

题目链接:https://codeforces.com/contest/1934/problem/D1

一道位运算的题,只要找出初始数n转为二进制后第一二个1的位置和目标数第一个1的位置的关系即可解决;

/***
 *    author:  touirst
 *    created: now
***/
#include <bits/stdc++.h>

using namespace std;

using ll=long long;
const int N=1e9+5;

void save_the_newnew() {
    ll n,m;
    cin>>n>>m;
    //cout<<n<<"\n";
    //cout<<(n^m)<<"\n";
    if((n^m)<n){cout<<1<<"\n"<<n<<" "<<m<<"\n";return;}
    ll lim{0},bn{63},bm{63};
    vector<int> a(65),b(65);
    //cout<<n<<"\n";
    while(!((n>>bn)&1)){
        bn--;
    }
    //cout<<bn<<"\n";
    bn--;
    //cout<<bn<<"\n";
    while(!((n>>bn)&1)){
        bn--;
    }
    //cout<<bn<<"\n";
    lim=(1ll<<(bn+1))-1;
    //cout<<lim<<"\n";
    while(!((m>>bm)&1)){
        bm--;
    }
    if(lim==m){
        cout<<1<<"\n"<<n<<" "<<m<<"\n";return;
    }else if(lim<m||lim>=n){
        cout<<"-1\n";return;
    }else{
        cout<<2<<"\n"<<n<<" "<<lim<<" "<<m<<"\n";
    }
    //cout<<bm<<"\n";
    //cout<<((7>>0)&1)<<" "<<((7>>1)&1)<<" "<<((7>>2)&1);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int ttt{1};
    cin>>ttt;
    while(ttt--){
        save_the_newnew();
    }
}
posted on   漫卷  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示