codeforce刷题 1

1.Sequence Pair Weight

题目大意
给定一个长度为 n 的数组 a,规定 i < j 且 ai = aj 为一个数对,求数组 a 的 所有连续子序列中有多少个这样的数对,输出总数。
解题思路
易得如果一个一个子序列去遍历的话,那么大约有 n*n/2 个子序列,会超时所以要考虑更简单的方法,采用动态规划的思想,dp[i] 代表以第 i 个数为结尾的所有子序列的答案总数,同时用 map 储存每一个数字出现的次数

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<stack>
using namespace std;
typedef unsigned long long ll;
typedef pair<ll, ll> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
const int M = 1e6 + 10;
int u, d, ans = 0;
bool cann = 0;
char s[N];
int a[N];
vector<int> G[N];
int dist[N];
bool vis[N];
ll dp[N];
int main(){
   
    std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
   
        ll n, fin_ans = 0;
        cin >> n;
        map<ll, ll> pp;
        for (int i = 1; i <= n;i++){
   
            cin >> a[i];
        }
        if(n==1){
   
            cout << 0 << endl;
            continue;
        }
        dp[1] = 0;
        pp[a[1]]++;
        for (int i = 2; i <= n;i++)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值