TOJ winter vacation special training 1-STL

Today, I didn't have to train. I spent the whole afternoon in AK training 1.

At the beginning, I had a feeling that I was old and honest... I had been reviewing for a long time at the end of the term... Did AC not go well

Fortunately, the feeling came back soon

ABCD is Vector, see the title is given the main function, I know that the event is not good

Use vector < int > & VEC when passing values, otherwise the copy function will not be called.

I didn't clear the stack at the beginning. I saw that the output elements were duplicated without even thinking about it. Then the end of EOF was controlled by Bool.

bool Input(vector<int> &vec)
{
	vec.clear();
	int n,m,i;
	if(scanf("%d",&n)==EOF)
	return false;
	for(i=0;i<n;i++)
	{
		scanf("%d",&m);
		vec.push_back(m);
	}
	return true;
}
bool cmp(const int &a,const int &b)
{
    return a>b;
}
void Sort(vector<int> &vec)
{
	sort(vec.begin(),vec.end(),cmp);
}


WA also silly husband to ask teachers not to heavy... So embarrassed... So, every time before processing remember vec.clear();

The E question is stack. After three rounds of RE, I found that it's pop. I forgot to judge it's not empty. Remember if(!st.empty())

Question G priority queue is from large to small by default

The priority queue of question H needs to be typed from small size to be defined as priority [queue < char, vector < char >, greater < char > > Qu;

I did the J problem for a long time, map sorting. Map is sorted by Key value by default, and topics are also required to be sorted by Vaule, so a vector is opened to store and sort.

typedef pair<string, int> PAIR;  
int cmp(const PAIR& x, const PAIR& y)  
{  
    return x.second < y.second;  
} 
void SortOutput(map<string, int> &sm)
{
	//string 
	map<string, int>::iterator it;
 	for(it=sm.begin();it!=sm.end();it++)
  	cout<<it->first<<" "<<it->second<<endl;
  	//int
  	vector<PAIR> vec;  
  	map<string,int>::iterator it2;
	for(it2=sm.begin();it2!=sm.end();it2++)  
 	vec.push_back(make_pair(it2->first,it2->second));  
	sort(vec.begin(),vec.end(),cmp); 
	vector<PAIR>::iterator it3;
	for(it3=vec.begin();it3!=vec.end();it3++)   
  	cout<<it3->first<<" "<<it3->second<<endl;
}
I didn't sort out the K questions first, but I output one for a long time... So I remember the sort first.

I've used count before. Post it

int Find(vector<int> &vec,int x)
{
	return count(vec.begin(),vec.end(),x);
}
In question M, the value of int obtained by using Lower_bound is not an int value and cannot be output directly, so use pos-vec.begin() to output (+ 1 is the position starts from 1).

void PrintFind(vector<int> &vec,int x)
{
	sort(vec.begin(),vec.end()); 
	vector<int>::iterator pos;
	pos=lower_bound(vec.begin(),vec.end(),x);
	if(pos!=vec.end())
	printf("%d\n",pos-vec.begin()+1);
	else
	printf("None\n");
}
O problem also did for a while.. priority queue sorting.. changed the thinking, write sorting in the structure, overload function almost won't be... C + + learned in vain

struct Point
{
    int x,y;
    bool operator < (const Point &a) const
    {
    	if(x!=a.x)
        return x>a.x;
        else
        return y>a.y;
    }
}point;
priority_queue<Point> qu;
void Input()
{
	int n,i;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&point.x,&point.y);
		qu.push(point);		
	}
}








Posted on Sun, 03 May 2020 16:00:14 -0700 by stublackett