2020 byte beat first campus recruitment exams solution algorithm direction

first question

[Title] is intended to give <a, b>, can be understood as a dad is b, now you have to turn all the output of each father's son, the son of sorting between lexicographical order

[Thinking] ideas is not difficult, with a map to map the father's name to a number, and then built a two-dimensional vector, son push_back to the corresponding father back, then you can sort the output.

[Pit] actually duplicate the last three minutes to find a little pit ah! That is a father is a b remark many times, then the vector will be repeated in the elements, you need to de-emphasis, of course, directly set to automatically re on the line

Here Insert Picture Description

[Code]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
const int maxn=1005;
const int INF=0x3f3f3f3f;
const ll mod=998244353;

int n;
int a[maxn];
string s,tmp;
map<string,int>mp;
vector<string>ans[maxn];
vector<string>vec;
set<string>S[maxn];

int main()
{
    cin>>n;
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        cin>>s>>tmp;
        if(mp[tmp]==0) mp[tmp]=++cnt,vec.push_back(tmp);
        S[mp[tmp]].insert(s);
    }
    for(int i=0;i<cnt;i++)
    {
        cout<<vec[i];
        //sort(ans[i+1].begin(),ans[i+1].end());
        //for(int j=0;j<ans[i+1].size();j++) cout<<" "<<ans[i+1][j];
        set<string>::iterator it;
        for(it=S[i+1].begin();it!=S[i+1].end();it++) cout<<" "<<*it;
        puts("");
    }
}

The second question

When [thinking] as to make the least number of refueling, apparently to use greedy, every time the oil is not sufficient to reach the current gas station, the foregoing description is necessary to pay more once the oil, to add the words must be added to the gas station in front of the largest oil, so with a priority queue to maintain it, to pop up every time the maximum value

[Code]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
const int maxn=1005;
const int INF=0x3f3f3f3f;
const ll mod=998244353;

int n,d,w;
int a[maxn];
string s;
int dis[maxn];

int main()
{
    scanf("%d%d",&d,&w);
    getchar();
    getline(cin,s);
    int len=s.size();
    int n=1,cnt=0;
    for(int i=0;i<len;i++)
    {
        if(s[i]>='0'&&s[i]<='9') cnt=cnt*10+s[i]-'0';
        else
        {
            dis[n++]=cnt;
            cnt=0;
        }
    }
    dis[n++]=cnt;
    //printf("%d\n",n);
    dis[0]=0;
    dis[n+1]=d;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int ans=0;
    priority_queue<int,vector<int>,less<int> >q;
    for(int i=1;i<=n+1;i++)
    {
        if(w>=dis[i]) q.push(a[i]);
        else
        {
            while(q.size())
            {
                int x=q.top();
                w+=x;
                ans++;
                q.pop();
                if(w>=dis[i])
                {
                    q.push(a[i]);
                    break;
                }
            }
        }
    }
    if(w>=d) printf("%d\n",ans);
    else puts("-1");
}

The third question

[Thinking] classic maze, only one more portal, feel free to use anything you can to connect between the two portals, and then bfs wide search, every time when a program is the portal to go to the surrounding , there is walked to the other side of the portal

But for some reason it has been 87.5 times out, check for a long time did not check out QAQ

[PS] have dalao reminded, should I go because when there is no channel vis judge ...

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
const int maxn=205;
const int INF=0x3f3f3f3f;
const ll mod=998244353;

int n,m;
int mp[maxn][maxn];
int vis[maxn][maxn];
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
vector<pair<int,int> >vec[maxn*maxn];
map<int,int>kk;

struct node
{
    int x,y,step;
}now,nex;

int bfs(int sx,int sy)
{
    vis[sx][sy]=1;
    queue<node>q;
    q.push({sx,sy,0});
    //printf("%d\n",mp[0][0]);
    while(q.size())
    {
        now=q.front();
        q.pop();
        //printf("%d %d %d %d\n",now.x,now.y,now.step,mp[now.x][now.y]);
        if(mp[now.x][now.y]==-3) return now.step;
        if(mp[now.x][now.y]>0)
        {
            //puts("**");
            int x=kk[mp[now.x][now.y]];
            if(vec[x][0].first==now.x&&vec[x][0].second==now.y)
            {
                nex.x=vec[x][1].first;
                nex.y=vec[x][1].second;
                nex.step=now.step+1;
                q.push(nex);
                vis[nex.x][nex.y]=1;
            }
            else
            {
                nex.x=vec[x][0].first;
                nex.y=vec[x][0].second;
                nex.step=now.step+1;
                q.push(nex);
                vis[nex.x][nex.y]=1;
            }
             //printf("@@%d %d\n",nex.x,nex.y);
        }
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+dir[i][0];
            nex.y=now.y+dir[i][1];
            nex.step=now.step+1;
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&mp[nex.x][nex.y]!=-1&&vis[nex.x][nex.y]==0)
            {
                q.push(nex);
                vis[nex.x][nex.y]=1;
            }
        }
    }
    return -1;
}

int main()
{
    scanf("%d%d",&m,&n);
    int sx,sy;
    int cnt=1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            scanf("%d",&mp[i][j]);
            vis[i][j]=0;
            if(mp[i][j]==-2) sx=i,sy=j;
            if(mp[i][j]>0)
            {
                if(kk[mp[i][j]]==0) kk[mp[i][j]]=cnt++;
                vec[kk[mp[i][j]]].push_back(make_pair(i,j));
            }
        }
    }
    //printf("%d %d\n",sx,sy);
    printf("%d\n",bfs(sx,sy));
}


Fourth Question

[Title] idea is to have dependency tree model DP (backpack), connected directly to the subject of the relationship between two edge points, but may not form a tree (tree plurality of particles dispersed), the introduction of dummy node (when the dependency of the point 0, with 0 to this point even the like) as the roots, it will be dispersed and the other connected to compose a tree roots can be a tree dp

With DP [i] [j] i is expressed in the root, with a maximum value of the j-th node, from bottom up and update

The final answer is dp [0] [m + 1]

[Code]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
const int maxn=205;
const int INF=0x3f3f3f3f;
const ll mod=998244353;

int n,m;
int val[maxn];
int dp[maxn][maxn];
vector<int>vec[maxn];

void dfs(int u)
{
    for(int i=0;i<vec[u].size();i++)
    {
        int v=vec[u][i];
        dfs(v);
        for(int j=m-1;j>=0;j--)
        for(int k=0;k<=j;k++)
        {
            dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]); //这里是还没算当前树根u,所以总和是m-1
        }
    }
    for(int i=m;i>=1;i--) dp[u][i]=dp[u][i-1]+val[u];
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d%d",&x,&val[i]);
        vec[x].push_back(i);
    }
    m++;
    mst(dp,0);
    dfs(0);
    printf("%d\n",dp[0][m]);
}

Published 259 original articles · won praise 100 · views 130 000 +

Guess you like

Origin blog.csdn.net/my_sunshine26/article/details/104886835