pat-1129

I can only say it is awesome

My thoughts are too many for loops after timeout

#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
struct node{
	int index,fre;
}; 
bool cmp(node&a,node&b){
	if(a.fre!=b.fre) return a.fre>b.fre;
	else return a.index<b.index;
}
vector<int> a;
vector<node> ans;


int main(){
	int n,m,temp;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>temp;
		a.push_back(temp);
	}
	for(int i=1;i<a.size();i++){
		ans.clear();
		unordered_map<int,int> frr;
		set<int>num;
		for(int j=i-1;j>=0;j--){
			
		num.insert(a[j]);
			frr[a[j]]++;
	
		
		/*if(num.size()==m||j==0){//注意到最开始元素的情况 异常的output例子就是要注意的地方 
			for(auto it=num.begin();it!=num.end();it++){
				ans.push_back({*it,frr[*it]} );
			}
			sort(ans.begin(),ans.end(),cmp);
			for(int q=0;q<ans.size();q++){
				if(q==0) printf("%d:",a[i]);
				printf(" %d",ans[q]);
				if(q==ans.size()-1) printf("\n");
			}
			break;
		}*/
		}
		for(auto it=num.begin();it!=num.end();it++){
				ans.push_back({*it,frr[*it]} );
			}
			sort(ans.begin(),ans.end(),cmp);
			int y=ans.size();
			  int mn=min(y,m);
				for(int q=0;q<mn;q++){
				if(q==0) printf("%d:",a[i]);
				printf(" %d",ans[q]);
				if(q==mn-1) printf("\n");
			}
		
	}
	return 0;
}

Big idea

#include<iostream>
#include<set>
using namespace std;
struct node{
	int index,fre;
	bool operator <(const node&a)const{//重载这要加const,两个(注意) 
		return (fre!=a.fre) ? fre>a.fre: index<a.index;
	}
};
int book[50009];
set<node> ans;
int main(){
	int n,m,temp;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		int cnt=0;
		scanf("%d",&temp);
		if(i!=0){
			printf("%d:",temp);
			for(auto it=ans.begin();it!=ans.end()&&cnt<m;it++){
				printf(" %d",it->index);//set里面访问node元素it->index(注意) 
				cnt++;
			}
			printf("\n");
		}
		auto  pos=ans.find(node {temp,book[temp]});//找结点要加node,前面要是auto(注意) 
		if(pos!=ans.end())ans.erase(pos);//有可能为空就会出现错误 
		book[temp]++;
		ans.insert({temp,book[temp]});
	}
	return 0;
}

Summary 1. My idea is to use set as an index, use the key-value pair map to do a correspondence with the number, and finally use sort to sort in the vector of the structure node.

2. The idea of ​​the big guy, overloading the less than sign <, the order of the realization in the set is the order of the answer, and make full use of the law in the question, each time adding one only changes the number of times that the number is increased, and the previous one does not need to be changed , A lot less repetitive work, I just re-statistics every time, so RE

3.bool operator <(const node&a)const{//Overload this must add const, two (note)

4. printf(" %d",it->index);//Access the node element in set it->index (note) 

5. auto pos=ans.find(node ​​{temp,book[temp]});//To find the node, add node, if the front is auto (note) 

6.if(pos!=ans.end())ans.erase(pos);//If it is empty, an error will occur

English

1.

Problem Operator overloading is not clear

 

 

 

Guess you like

Origin blog.csdn.net/m0_45359314/article/details/113061325