吼 困 鸭

先给大家分享一道CF的题,这个题用结构体搞很复杂,看了题解用优先队列感觉很巧妙,枚举变换个数,削弱比他强的队伍的个数,然后剩下的再按从小到大弹出。

C. Elections
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.

Elections are coming. You know the number of voters and the number of parties — nn and mm respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give ii-th votercici bytecoins you can ask him to vote for any other party you choose.

The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party.

Input

The first line of input contains two integers nn and mm (1n,m30001≤n,m≤3000) — the number of voters and the number of parties respectively.

Each of the following nn lines contains two integers pipi and cici (1pim1≤pi≤m, 1ci1091≤ci≤109) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision.

The United Party of Berland has the index 11.

Output

Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.

Examples
input
Copy
1 2
1 100
output
Copy
0
input
Copy
5 5
2 100
3 200
4 300
5 400
5 900
output
Copy
500
input
Copy
5 5
2 100
3 200
4 300
5 800
5 900
output
Copy
600
Note

In the first sample, The United Party wins the elections even without buying extra votes.

In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties 33, 44 and 55 get one vote and party number 22 gets no votes.

In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.

#include<bits/stdc++.h>
using namespace std;
const int MAXN=3007;
int n,m,i,j,k;
long long ans=1e18,cnt;
priority_queue<int,vector<int>,greater<int> >e[MAXN],s[MAXN],S;
int main()
{
    cin>>n>>m;
    for(i=1; i<=n; ++i)
    {
        scanf("%d%d",&j,&k);
        s[j].push(k);
    }
    for(i=1; i<=n; ++i)
    {
        while(!S.empty())
            S.pop();
        cnt=s[1].size();
        for(j=2; j<=m; ++j)
        e[j]=s[j];
        long long v=0;
        for(j=2; j<=m; ++j)
        {
            while(e[j].size()>=i)
            {
                v+=e[j].top();
                e[j].pop();
                cnt++;
            }
        }
        for(j=2; j<=m; ++j)
        {
            while(!e[j].empty())
            {
               S.push(e[j].top());
                e[j].pop();
            }
        }
        for(; cnt<i&&!S.empty(); ++cnt)
        {
            v+=S.top();
            S.pop();
        }
        if(cnt>=i) ans=min(ans,v);
    }
   cout<<ans<<endl;
    return 0;
}

这道题,我用的栈做的,感觉是一道好题

 

我的代码:

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
using namespace std;
stack<int>kepa;
int main()
{
    int n,m,j,a;
    int t=1;
    string s;
    cin>>n>>m;
    kepa.push(0);
    for(int i=1;i<=m;i++)
    {
        int it=kepa.top();
        int res=1;
        int temp=0;
        cin>>s;
        if(s[0]=='u')
        {
            cin>>a;
            while(a--)
            {
                kepa.pop();
            }
        }
        else
        {
            if(s[0]=='-')
            {
                res=-1;
            }
            for(j=0;j<s.length();j++)
            {
                if(j==0&&res==-1)
                {
                    continue;
                }
                    temp=temp*10+s[j]-'0';
            }
          temp=temp*res;
            it+=temp;
            while(it<0)
                it+=n;
                it%=n;
                kepa.push(it);
        }
    }
    cout<<kepa.top()<<endl;
    return 0;
}

然后看了pzr的做法,他用数组做的,atoi是字符串转成整数,s.c_str()专用str

pzr的代码:

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
using namespace std;
int main()
{
    int n,m;
    int t=1;
    int num[1000];
    num[0]=0;
    string s;
    int a,b;
    int sw;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>s;
        if(s[0]=='u'){
            cin>>a;
            t-=a+1;
        }
        else{
            a=atoi(s.c_str());
            sw=num[t-1];
            sw+=a;
            sw%=n;
            if(sw<0) sw+=n;
            num[t]=sw;
        }
        t++;
    }
    cout<<num[t-1]<<endl;
    return 0;
}

这道题,用dp求解每一步的最优子结构,dp[i][j]表示前i天的情况,j表示第i天的情况

#include <iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[105][105],w[105],pre[105];
int main()
{
int n,m,i,j;
cin>>n>>m;
for(i=1;i<=n;i++)
    cin>>w[i];
pre[0]=m;
for(i=1;i<=n;i++)
{
    pre[i]=pre[i-1]*2/3;
}
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(i=1;i<=n;i++)
{
    dp[i][0]=min(w[i],pre[0]);
}
for(i=0;i<=n;i++)
{
    for(j=0;j<=n;j++)
    {
        if(dp[i][j]==-1)
            continue;
        dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+min(w[i+1],pre[j+1]));
         dp[i+2][j]=max(dp[i+2][j],dp[i][j]+min(w[i+2],pre[j]));
          dp[i+3][0]=max(dp[i+3][0],dp[i][j]+min(w[i+3],pre[0]));
    }
}
int ans=0;
    for(j=0;j<=n;j++)
    {
        ans=max(ans,dp[n][j]);
    }

cout<<ans<<endl;
return 0;
}
原文地址:https://www.cnblogs.com/kepa/p/9480053.html