Merge Equals (STL)

本文介绍了一种利用STL中的map数据结构解决特定序列操作问题的方法,通过记录元素出现位置并更新,实现了对序列的高效处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一看题目吓一跳,仔细一想是水题

题意很简单(以下转自洛谷):

给定正整数序列 a[1...n]a[1...n]a[1...n],每次你需要找到序列中出现次数 >=2>=2>=2 的最小值 xxx,找到 xxx222 个最小下标 i,j{i,j}i,j,删除 a[i]a[i]a[i],将 a[j]a[j]a[j] 改为 2x2x2x,反复进行如此操作,求序列最终的形态。

查询最靠前的两个数并合并这件事感觉好复杂

但是显然这个东西就是记录一下上次出现的位置就行了

数字大小 10910^9109,数组怎么开的下?

简单, STL\mathsf{STL}STL 里的 map\mathsf{map}map 可以解决!

然后就搞定了

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(s) freopen(s".in", "r", stdin), freopen(s."out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 150010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int n, ans ;
mii m ;
int a[N] ;

signed main(){
    scanf("%lld", &n) ; ans = n ;
    rep(i, 1, n) scanf("%lld", &a[i]) ;
    rep(i, 1, n) {
    	while (m[a[i]]) {
    		a[m[a[i]]] = 0 ;
    		m[a[i]] = 0 ;
    		a[i] *= 2 ;
			ans-- ;
		}
		m[a[i]] = i ;
	}
	cout << ans << endl ;
    rep(i, 1, n) if (a[i]) cout << a[i] << " " ;
	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/


题目描述 现有一个空整数序列 A = ( ) A=()。你需要按顺序处理 Q Q个查询操作,查询分为两种类型: 类型 1 1:格式为1 c x。将 c c个 x x添加到 A A的末尾。 类型 2 2:格式为2 k。从 A A中移除前 k k个元素,并输出被移除的 k k个整数之和。保证此时序列长度不小于 k k。 约束条件 1 ≤ Q ≤ 2 × 10 5 1≤Q≤2×10 5 在类型 1 1查询中, 1 ≤ c ≤ 10 9 1≤c≤10 9 在类型 1 1查询中, 1 ≤ x ≤ 10 9 1≤x≤10 9 在类型 2 2查询中,设此时 A A的长度为 n n,则 1 ≤ k ≤ min ⁡ ( 10 9 , n ) 1≤k≤min(10 9 ,n) 所有输入值均为整数 输入格式 输入通过标准输入给出,格式如下: Q Q query 1 query 1 ​ query 2 query 2 ​ ⋮ ⋮ query Q query Q ​ 其中 query i query i ​ 表示第 i i个查询,其格式为以下两种之一: 1 1 c c x x 2 2 k k 输出格式 设共有 q q个类型 2 2查询。输出 q q行, 第 i i行应包含对第 i i个类型 2 2查询的应答结果。 样例1 Inputcopy Outputcopy 5 1 2 3 1 4 5 2 3 1 6 2 2 5 11 19 第 1 1次查询:将 2 2个 3 3添加到 A A末尾。此时序列变为 A = ( 3 , 3 ) A=(3,3) 第 2 2次查询:将 4 4个 5 5添加到 A A末尾。此时序列变为 A = ( 3 , 3 , 5 , 5 , 5 , 5 ) A=(3,3,5,5,5,5) 第 3 3次查询:移除 A A中前 3 3个元素。被移除的 3 3个整数之和为 3 + 3 + 5 = 11 3+3+5=11,故输出 11 11。移除后序列为 A = ( 5 , 5 , 5 ) A=(5,5,5) 第 4 4次查询:将 6 6个 2 2添加到 A A末尾。此时序列变为 A = ( 5 , 5 , 5 , 2 , 2 , 2 , 2 , 2 , 2 ) A=(5,5,5,2,2,2,2,2,2) 第 5 5次查询:移除 A A中前 5 5个元素。被移除的 5 5个整数之和为 5 + 5 + 5 + 2 + 2 = 19 5+5+5+2+2=19,故输出 19 19。移除后序列为 A = ( 2 , 2 , 2 , 2 ) A=(2,2,2,2) 样例2 Inputcopy Outputcopy 10 1 75 22 1 81 72 1 2 97 1 84 82 1 2 32 1 39 57 2 45 1 40 16 2 32 2 42 990 804 3024 样例3 Inputcopy Outputcopy 10 1 160449218 954291757 2 17217760 1 353195922 501899080 1 350034067 910748511 1 824284691 470338674 2 180999835 1 131381221 677959980 1 346948152 208032501 1 893229302 506147731 2 298309896 16430766442004320 155640513381884866 149721462357295680 cpp: #include <bits/stdc++.h> #define int long long using namespace std; int q; signed main() { cin >> q; queue<int> a; while (q--) { int n; cin >> n; if (n == 1) { int c, x; cin >> c >> x; for (int i = 1; i <= c; i++) a.push(x); } else if (n == 2) { int k, sum = 0; cin >> k; for (int i = 1; i <= k; i++) { sum += a.front(); a.pop(); } cout << sum << endl; } } return 0; } 优化cpp
最新发布
07-12
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值