D. Merge Equals ###K ###K //K
题目链接:https://codeforces.ml/contest/962/problem/D
题意:给定数组 每个数 首先出现的两次合并成一个数 位置在第二次出现的位置 问最后的数组
思路:合并的次数最多n-1次 1~n扫一遍 如果能找到上次出现的位置就把当前的位置数更新,把上次的位置去掉
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=2e5+10; 4 const int mod=1e9+7; 5 #define ll long long 6 #define pi pair<int,int> 7 #define fi first 8 #define sc second 9 #define pb push_back 10 11 12 int st[maxn]; 13 ll a[maxn]; 14 map<ll,int>mp; 15 16 17 18 int main() 19 { 20 ios::sync_with_stdio(0); 21 cin.tie(0); 22 int n; 23 cin>>n; 24 for(int i=1;i<=n;i++) 25 { 26 cin>>a[i]; 27 while(mp[a[i]]) 28 { 29 st[mp[a[i]]]=1; 30 mp[a[i]]=0; 31 a[i]*=2; 32 } 33 mp[a[i]]=i; 34 } 35 vector<ll>ans; 36 for(int i=1;i<=n;i++) 37 { 38 if(!st[i]) ans.pb(a[i]); 39 } 40 int len=ans.size(); 41 cout<<len<<'\n'; 42 for(int i=0;i<len;i++) 43 { 44 if(i) cout<<" "; 45 cout<<ans[i]; 46 } 47 cout<<'\n'; 48 49 50 }

浙公网安备 33010602011771号