对于map的新应用

题源codeforces1974 problemC

题目大意

定义当两个三元组A和B中,满足三元组中有且仅有两个元素相等,比如
a 1 = b 1 , a 2 = b 2 , a 3 ! = b 3 a_1=b_1,a_2=b_2,a_3!=b_3 a1=b1,a2=b2,a3!=b3
这只是一种情况,三种情况之一

解题思路

暴力。
挨个遍历这个数组中的所有三元组,把这些三元组中前两个元素/后两个元素/两边两个元素,用

map<pair<int,int>,int>

记录下来,然后如果重复出现,就加上。
去重。
挨个遍历这个数组中的所有三元组,把这些三元组全部用

map<tuple<int,int,int> ,int>

然后记录下来所有重复出现的三元组,重复次数也记录,便于去重。
详情请看代码

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define For for (int i = 1; i <= n; i++)
#define rFor for (int i = n; i > 0; i--)
#define rep(i, sta, end) for (int i = sta; i <= end; i++)
#define rrep(i, end, sta) for (int i = end; i >= sta; i--)
#define ALL(x) for (auto item : x)


inline void solve() {
	int n;cin>>n;vector<int> arr(n+10);
	For cin>>arr[i];
	ll ans=0;
	map<pair<int,int> ,int> count_of_pair;
	//Iterate over all triples of the entire array
	rep(i,1,n-2)
		ans+=count_of_pair[{arr[i],arr[i+1]}]++;
	//cout<<ans<<endl;
	count_of_pair.clear();
	rep(i,1,n-2)
		ans+=count_of_pair[{arr[i+1],arr[i+2]}]++;
	//cout<<ans<<endl;
	count_of_pair.clear();
	rep(i,1,n-2)
	    ans+=count_of_pair[{arr[i],arr[i+2]}]++;
	//cout<<ans<<endl;
	count_of_pair.clear();
	map<tuple<int,int,int>,int> count_of_triple;
	rep(i,1,n-2){
			ans-=3*count_of_triple[{arr[i],arr[i+1],arr[i+2]}]++;
	}
	//cout<<ans<<endl;
	cout<<max(ans,0LL)<<endl;
	//cout<<endl;
}



int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int num = 1;
	cin >> num;
	while (num--) {
		//cout << "Main function execute properly before solve function " << endl;
		solve();
		//	cout << "Main function execute properly after solve function" << endl;
	}
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值