凌波微步

题目链接:https://ac.nowcoder.com/acm/problem/14346
方法一:
用小根堆存下每个数,然后遍历小根堆,如果当前堆顶元素比前一个堆顶元素大的话就+1
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+10; vector<ll>arr(maxn); priority_queue<ll, vector<ll>,greater<ll> >q1; int main(){ int t; cin>>t; while(t--){ int n; cin>>n; for(int i=0;i<n;i++){ ll num; cin>>num; q1.push(num); } ll temp=0; int ans=0; while(!q1.empty()){ if(temp==0){ ans++; temp=q1.top(); q1.pop(); }else if(temp==q1.top()){ q1.pop(); continue; }else{ temp=q1.top(); q1.pop(); ans++; } } cout<<ans<<endl; } return 0; }

方法二:set
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+10; set<ll>st; int main(){ int t; cin>>t; while(t--){ int n; cin>>n; st.clear(); for(int i=0;i<n;i++){ ll num; cin>>num; st.insert(num); } cout<<st.size()<<endl; } return 0; }

 

 
posted @ 2021-07-28 15:16  ZangYiDe  阅读(104)  评论(0)    收藏  举报