大一集训--考试--队列 / 优先队列 / 线性筛

队列 / 优先队列 / 线性筛

题目一:P1295

#include 

using namespace std;
struct num
{
    int x;
    long long change;
};
struct cmp1
{
    bool operator ()(num a,num b)
    {
        return a.change>b.change;
    }
};
long long be(int x);
int main()
{
    int n;
    while(cin >> n)
    {
        priority_queue<num,vector<num>,cmp1>que;
        int x;
        struct num save;
        for(int i=1; i<=n; i++)
        {
            cin >> x;
            save.x=x;
            save.change=be(x);
            que.push(save);
        }
        while(que.size()!=1)
        {
            cout << que.top().x << " ";
            que.pop();
        }
        cout << que.top().x << endl ;
        que.pop();
    }
    return 0;
}
long long be(int x)
{
    if(x==0)
        return 0;
    long long ans=0;
    while(x)
    {
        ans*=10;
        ans+=x%10;
        x/=10;
    }
    return ans;
}

题目二:P1311

#include 

using namespace std;
int main()
{
    int t;
    int n;;
    cin >> t;
    int a[100];
    while(t--)
    {
        memset(a,0,sizeof(a));
        int flag=0;
        queue<int>que;
        cin >> n;
        for(int i=1;i<=n;i++)
        {
            que.push(i);
        }
        while(que.size()!=1)
        {
            a[++flag]=que.front();
            que.pop();
            que.push(que.front());
            que.pop();
        }
        if(flag)
        {
            for(int i=1;i<=flag;i++)
            {
                if(i==1)
                {
                    cout << a[i];
                }
                else
                {
                    cout << "," << a[i];
                }
            }
            cout << endl ;
        }
        cout << que.front() << endl ;
    }
    return 0;
}

题目三:P2114

#include 

using namespace std;
const int maxx=1e6+5;
int vis[maxx];
int ans[maxx];
int flag=0;
void ss(int n);
int main()
{
    int t;
    ss(1000000);
    cin >> t;
    int l,r;
    long long sum=0;
    while(t--)
    {
        cin >> l >> r;
        int s1,s2;
        int flag=0;
        for(int i=l;i<=r;i++)
        {
            if(vis[i])
            {
                s1=i;
                flag=1;
                break;
            }
        }
        if(!flag)
            s1=0;
        flag=0;
        for(int i=r;i>=l;i--)
        {
            if(vis[i])
            {
                s2=i;
                flag=1;
                break;
            }
        }
        if(!flag)
            s2=0;
        sum+=(s1+s2);
    }
    cout << sum << endl ;
    return 0;
}
void ss(int n)
{
    memset(vis,1,sizeof(vis));
    vis[0]=vis[1]=0;
    for(int i=2;i<=n;i++)
    {
        if(vis[i])
            ans[++flag]=i;
        for(int j=1;j<=flag&&i*ans[j]<=n;j++)
        {
            vis[i*ans[j]]=0;
            if(i%ans[j]==0)
                break;
        }
    }
}

题目四:P2110

#include 

using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
        long long x;
        priority_queue<long long,vector<long long>,greater<long long> >que;
        for(int i=1; i<=n; i++)
        {
            cin >> x;
            que.push(x);
        }
        long long ans=0;
        long long save;
        int flag=0;
        while(que.size()!=1)
        {
            save=que.top();
            que.pop();
            save+=que.top();
            que.pop();
            que.push(save);
            if(!flag)
            {
                cout << save;
                flag=1;
            }
            else
                cout << " " << save;
        }
        cout << endl ;
    }
    return 0;
}

题目五:P2111

#include 

using namespace std;
struct _people
{
    int num;
};
const int maxx=1e7+5;
int vis[maxx];
int ans[maxx];
int flag=0;
void ss(int n);
int main()
{
    int t;
    int n;
    ss(10000000);
    cin >> t;
    while(t--)
    {
        cin >> n;
        queue<_people>que;
        queue<_people>que2;
        struct _people save;
        for(int i=1;i<=n;i++)
        {
            save.num=i;
            que.push(save);
        }
        while(que.size()!=1)
        {
            int now=1;
            while(!que.empty())
            {
                if(vis[now])
                {
                    que2.push(que.front());
                }
                now++;
                que.pop();
            }
            que=que2;
            while(!que2.empty())
            {
                que2.pop();
            }
        }
        cout << que.front().num << endl ;
    }
    return 0;
}
void ss(int n)
{
    memset(vis,1,sizeof(vis));
    vis[0]=vis[1]=0;
    for(int i=2;i<=n;i++)
    {
        if(vis[i])
            ans[++flag]=i;
        for(int j=1;j<=flag&&i*ans[j]<=n;j++)
        {
            vis[i*ans[j]]=0;
            if(i%ans[j]==0)
                break;
        }
    }
}

题目六:P2112

#include 

using namespace std;
const int maxx=1e7+5;
int vis[maxx];
int ans[maxx];
int ssnum[maxx];
int maxn[maxx];
int solve[maxx];
int flag=0;
void ss(int n);
int work(int x);
int main()
{
    int t;
    int n;
    ss(10000000);
    solve[0]=0;
    for(int i=1;i<=10000000;i++)
    {
        maxn[i]=work(i);
        if(maxn[i]<maxn[solve[i-1]])
            solve[i]=solve[i-1];
        else
            solve[i]=i;
    }
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%d\n",solve[n]);
    }
    return 0;
}
void ss(int n)
{
    memset(vis,1,sizeof(vis));
    vis[0]=vis[1]=0;
    for(int i=2;i<=n;i++)
    {
        if(vis[i])
            ans[++flag]=i;
        ssnum[i]=flag;
        for(int j=1;j<=flag&&i*ans[j]<=n;j++)
        {
            vis[i*ans[j]]=0;
            if(i%ans[j]==0)
                break;
        }
    }
}
int work(int x)
{
    if(vis[x]==0)
        return 0;
    int t=1;
    while(vis[ssnum[x]])
    {
        t++;
        x=ssnum[x];
    }
    return t;
}

题目七:P1949

#include 
struct line
{
    int x;
    int y;
    int len;
};
struct cmp1
{
    bool operator()(line a,line b)
    {
        return a.len>b.len;
    }
};
using namespace std;
int a[500005];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    priority_queue<line,vector<line>,cmp1>que;
    priority_queue<int,vector<int>,greater<int> >outque;
    priority_queue<int,vector<int>,greater<int> >shenzi;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(i!=1)
        que.push({i,i-1,a[i]-a[i-1]});
    }
    int out=0;
    struct line save;
    while(out<m&&!que.empty())
    {
        outque.push(que.top().len);
        save=que.top();
        que.pop();
        if(save.y>1)
        {
            save.y--;
            save.len=a[save.x]-a[save.y];
            que.push(save);
        }
        out++;
    }
    int x;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&x);
        shenzi.push(x);
    }
    int flag=1;
    while(!outque.empty())
    {
        if(shenzi.top()<outque.top())
        {
            flag=0;
            break;
        }
        shenzi.pop();
        outque.pop();
        if(shenzi.size()==0)
            break;
    }
    if(!flag||shenzi.size()!=0)
        printf("no\n");
    else
        printf("yes\n");
    return 0;
}

你可能感兴趣的:(c++入门,数据结构)