CCF 201909-4 Recommended system timeout &&100 points

This question is sorted out:
There are a total of m categories of products: 0~m-1 categories;
"For each new app user, each category of product initially has n products with different numbers": This sentence means that when you start typing, there are various products n identical products, the same includes the same product number and score.
Then you can perform operations: insert/delete/query
Insert: specify the category and number and score
Delete: specify the category and number
Query: query the top k products from all products, and search as far as possible (that is, how many can be found is the most But k)
And each type of product also has a number limit and there is also a number limit.
The principle is: (1) If the two scores are the same and in the same category, choose the smaller number. (2) If the two scores are the same and are not in the same category, select the smaller category number
.

The first is the timeout code, which scored 10 points:

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
struct Good{
    
    
    int  type;
    int  commodity;
    int  score;
};
class compare{
    
    
public:
    bool operator()(const Good g1,const Good g2)
    {
    
    
        if(g1.score == g2.score&&g1.type == g2.type)
        {
    
    
            return g1.commodity<g2.commodity;
        }else if(g1.score == g2.score&&g1.type != g2.type)
        {
    
    
            return g1.type<g2.type;
        }else
            return g1.score > g2.score;
    }
};
Good good;
int n,m,commodity,score;
set<Good,compare> goods;
set<Good,compare>::iterator it;
vector<int> chose_good[52];//存储各类已选商品的编号
int cns[52];//记录各类已选商品的个数
void my_print(Good good)
{
    
    
    cout<<good.type<<" "<<good.commodity<<" "<<good.score<<endl;
}
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,j,opnum,op,threshold,type_threshold[52],type,commodity;
    cin>>m>>n;
    for(i=0;i<n;i++)
    {
    
    
        cin>>commodity>>score;
        good.commodity=commodity;
        good.score    = score;
        for(j=0;j<m;j++)
        {
    
    
            good.type=j;
            goods.insert(good);
        }
    }
    cin>>opnum;
    for(i=0;i<opnum;i++)
    {
    
    
        cin>>op;
        switch(op){
    
    
        case 1:
            cin>>good.type>>good.commodity>>good.score;
            goods.insert(good);
            cout<<"添加后:"<<endl;
            for_each(goods.begin(),goods.end(),my_print);
            break;
        case 2:
            cin>>type>>commodity;
            for(it=goods.begin();it!=goods.end();it++)
            {
    
    
                if(it->type == type&&it->commodity == commodity) break;
            }
            if(it!=goods.end())
                goods.erase(it);
            cout<<"删除后:"<<endl;
            for_each(goods.begin(),goods.end(),my_print);
            break;
        case 3:
            for(j=0;j<52;j++)
            {
    
    
                cns[j]=0;
                chose_good[j].clear();
            }//cns[51]代表已选总数。
            cin>>threshold;
            for(j=0;j<m;j++)
            {
    
    
                cin>>type_threshold[j];//各类别的限制
            }
            for(it=goods.begin();cns[51]<threshold&&it!=goods.end();it++)
            {
    
    
                if(cns[it->type] < type_threshold[it->type])
                {
    
    
                    chose_good[it->type].push_back(it->commodity);
                    cns[51]++;
                    cns[it->type]++;
                }
            }
            for(j=0;j<m;j++)
            {
    
    
                if(chose_good[j].size()==0)
                {
    
    
                    cout<<"-1"<<endl;
                    continue;
                }
                for(vector<int>::iterator it=chose_good[j].begin();it!=chose_good[j].end();it++)
                {
    
    
                     if(it==chose_good[j].begin())
                        cout<<*it;
                    else
                        cout<<" "<<*it;
                }
                cout<<endl;
            }
            break;
        }
    }
    return 0;
}

Then there is the 100-point code, which refers to the idea of ​​"lazy deletion" of a certain tycoon. Although I don't know where the idea is, it does not time out. Didn't figure out how to save time (I also experienced the first query and then delete later?).

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
struct Good{
    
    
    int  type;
    int  commodity;
    int  score;
};

struct Del{
    
    
	int type;
	int id;

	Del(int _type, int _id){
    
    
		type=_type; id=_id;
	}

	bool operator < (const Del &rhs) const{
    
    
		if(type==rhs.type) return id<rhs.id;
		return type<rhs.type;
	}
};

class compare{
    
    
public:
    bool operator()(const Good g1,const Good g2)
    {
    
    
        if(g1.score == g2.score&&g1.type == g2.type)
        {
    
    
            return g1.commodity<g2.commodity;
        }else if(g1.score == g2.score&&g1.type != g2.type)
        {
    
    
            return g1.type<g2.type;
        }else
            return g1.score > g2.score;
    }
};
Good good;
int n,m,commodity,score;
set<Good,compare> goods;
set<Del> has_del;
set<Good,compare>::iterator it;
vector<int> chose_good[52];//存储各类已选商品的编号
int cns[52];//记录各类已选商品的个数
void my_print(Good good)
{
    
    
    cout<<good.type<<" "<<good.commodity<<" "<<good.score<<endl;
}
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,j,opnum,op,threshold,type_threshold[52],type,commodity;
    cin>>m>>n;
    for(i=0;i<n;i++)
    {
    
    
        cin>>commodity>>score;
        good.commodity=commodity;
        good.score    = score;
        for(j=0;j<m;j++)
        {
    
    
            good.type=j;
            goods.insert(good);
        }
    }
    cin>>opnum;
    for(i=0;i<opnum;i++)
    {
    
    
        cin>>op;
        switch(op){
    
    
        case 1:
            cin>>good.type>>good.commodity>>good.score;
            goods.insert(good);
            break;
        case 2:
            cin>>type>>commodity;
            has_del.insert(Del(type, commodity)); //将该商品加入删除表中,实行惰性删除
            break;
        case 3:
            for(j=0;j<52;j++)
            {
    
    
                cns[j]=0;
                chose_good[j].clear();
            }//cns[51]代表已选总数。
            cin>>threshold;
            for(j=0;j<m;j++)
            {
    
    
                cin>>type_threshold[j];//各类别的限制
            }
            for(it=goods.begin(); cns[51]<threshold && it!=goods.end();){
    
    
				//该类商品未选满,查看该商品是否已删除
				if(cns[(*it).type]<type_threshold[(*it).type]){
    
    
					if(has_del.find(Del((*it).type, (*it).commodity))!=has_del.end()){
    
     //存在于删除表中
						goods.erase(it++); //删除该元素,迭代器自增
					}
					//未删除
					else{
    
    
						++cns[(*it).type];
						++cns[51];
						chose_good[(*it).type].push_back((*it).commodity);
						it++;
					}
				}
				else it++;
			}
            for(j=0;j<m;j++)
            {
    
    
                if(chose_good[j].size()==0)
                {
    
    
                    cout<<"-1"<<endl;
                    continue;
                }
                for(vector<int>::iterator it=chose_good[j].begin();it!=chose_good[j].end();it++)
                {
    
    
                     if(it==chose_good[j].begin())
                        cout<<*it;
                    else
                        cout<<" "<<*it;
                }
                cout<<endl;
            }
            break;
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44142774/article/details/113481448