17级2019春季个人训练赛-3

A  Parentheses

Sample Input
3
18
(()())))(((((())((
2
()
8
(()))()(
Sample Output
4
0
2

题意:求转换括号使得字符串可以完全匹配。转换是左括号转换成右括号,右括号转换成左括号。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
const int MAX=1e6+7;
deque <char> dq;
stack <char> s;
int main()
{
    ll T,i,j,k,ans=0,n;
    cin>>T;
    char c;
    while(T--)
    {
        cin>>n;
        ans=0;
        while(!s.empty())
            s.pop();
        while(n--)
        {
            cin>>c;
            if(c=='(')
            {
                s.push(c);
            }
            else
            if(c==')')
            {
                if(!s.empty())
                {
                    if(s.top()=='(')
                        s.pop();
                    else
                        s.push(c);
                }
                else
                {
                    s.push(c);
                }
            }
        }
        ll p=0,a=0,b=0;
        //cout<<s.size()<<endl;
        while(!s.empty())
        {
            if(s.top()=='(')
            {
                a++;
                if(a==2)//出现((的情况
                {
                    ans++;
                    a-=2;
                }
                s.pop();
            }
            else
            {
                b++;
                if(b==2)//出现))的情况
                {
                    ans++;
                    b-=2;
                }
                else
                if(b==1&&a==1)//出现)(的情况
                {
                    ans+=2;
                    a--;
                    b--;
                }
                s.pop();
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

D - Discrete Logarithm Problem

Sample Input
31
24 3
3 15
0
Sample Output
7
21

题意:第一行输入p,之后每行输入a,b。输入a=0截止.(a^x)%p==b,计算x.x不存在输出0.

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <deque>
using namespace std;
typedef long long ll;
const int MAX=1e6+7;
deque <char> dq;
ll p;
ll fast(ll a,ll b)
{
    ll ans=1;
    a%=p;
    while(b)
    {
        if(b&1)
            ans=a*ans%p;
        a=a*a%p;
        b>>=1;
    }
    return ans%p;
}
int main()
{
    ll T,i,j,n,a,b,ans;
    cin>>p;
    while(cin>>a)
    {
        if(a==0)
            break;
        cin>>b;
        ll flag=0;
        for(i=0;i<p-1;i++)
        {
            ans=fast(a,i);
            if(ans==b)
            {
                cout<<i<<endl;
                flag=1;
                break;
            }
        }
        if(flag==0)
            cout<<"0"<<endl;
    }
    return 0;
}

K - Robots

Sample Input
1
1 10 1 2
Sample Output
11

题意:输入T组样例,之后每组样例输入x,y,n,m。x是第一种机器人,y是第二种机器人,n是第一种机器人的个数,m是第二种机器人的个数,要求n+m个机器人可以传输数据,x1可以将自己东西的传给y1,花费的时间是x;或者x1传给x2,花费x,y1传给x1,花费y……求只剩最后一位机器人身上有东西时,总的花费时间最少是多少。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
const int MAX=1e6+7;
deque <char> dq;
stack <char> s;
int main()
{
    ll T,i,j,k,x,y,n,m;
    cin>>T;
    while(T--)
    {
        cin>>x>>y>>n>>m;
        ll ans=0;
        if(x>y)
        {
            ans=(n-1)*x+y*m;
        }
        else
            ans=y*(m-1)+x*n;
        cout<<ans<<endl;
    }
    return 0;
}

以上是比赛内做出来的

C - Least Crucial Node 

INPUT
4
4
3
1 2
2 3
3 4

6
3
8
1 2
2 3
2 4
2 5
3 4
3 5
4 5
5 6

0

OUTPUT

3
2

题意:输入n(结点个数),k(起点),m(边的个数)。求最关键的点,最关键的意思是删掉该点使得起点与剩下的点联系更少。

找度最多的并且点最大的

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
const int MAX=1e6+7;
deque <char> dq;
stack <char> s;
ll mapp[110][110];
struct A
{
    ll flag=0;
    ll g=0;
}h[110];
bool cmp(struct A p,struct A q)
{
    if(p.flag==q.flag)
        return p.g>q.g;
    return p.flag>q.flag;
}
int main()
{
    ll T,i,j,k,x,y,n,m,a,b;
    while(cin>>n)
    {
        if(n==0)
            break;
        cin>>k>>m;
        memset(mapp,0,sizeof(mapp));
        for(i=1;i<=n;i++)
            h[i].g=i;
        for(i=0;i<m;i++)
        {
            cin>>a>>b;
            mapp[a][b]=1;
            mapp[b][a]=1;
            h[a].flag++;
            h[b].flag++;
        }
        sort(h+1,h+n+1,cmp);



//        for(i=1;i<=n;i++)
//        {
//            cout<<h[i].flag<<" "<<h[i].g<<endl;
//        }
//        cout<<"****"<<endl;

        for(i=1;i<=n;i++)
        {
            cout<<h[i].g<<endl;
            break;
        }
    }
    return 0;
}

还有一种方法,比较麻烦,但是保险。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define eps 0.0000000001
#define mem(a) memset(a,0,sizeof(a))
#define maxx 1e10
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int a[110][110];
int vis[110],dis[110];
int n,sink,m;
int Prim(int start)
{
    mem(vis);
    for(int i=1;i<=n;i++)
        dis[i]=a[start][i];
    dis[start]=0;
    vis[start]=1;
    int j;
    for(j=1;j<n;j++)
    {
        int mini=inf,k;
        for(int i=1;i<=n;i++)
        {
            if(dis[i]<mini&&vis[i]==0)
            {
                mini=dis[i];
                k=i;
            }
        }
        if(mini==inf)
            break;
        vis[k]=1;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0&&dis[i]>a[k][i])
                dis[i]=a[k][i];
        }
    }
    return j;
}
int main()
{
    while(cin>>n)
    {
        int ans=0,mm=inf;
        if(n==0)
            break;
        memset(a,inf,sizeof(a));
        cin>>sink>>m;
        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
            a[x][y]=a[y][x]=1;
        }
        int t[110][110];
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                t[i][j]=a[i][j];
        for(int i=1;i<=n;i++)
        {
            if(i==sink)
                continue;
            for(int j=1;j<=n;j++)//删掉该点
            {
                a[i][j]=inf;
                a[j][i]=inf;
            }
            int g=Prim(sink);
            //cout<<g<<endl;
            if(g<mm)
            {
                mm=g;
                ans=i;
            }

            for(int u=1;u<=n;u++)//赋为原值
                for(int v=1;v<=n;v++)
                    a[u][v]=t[u][v];
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值