第十五届浙江大学宁波理工学院程序设计大赛(同步赛)补题

https://ac.nowcoder.com/acm/contest/303#question

A签到了

#include
using namespace std;
typedef long long ll;
const int N = 1e6 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int main()
{
    int n;
    cin >> n;
    cout << 1998 + n;
    return 0;
}

B没读题上来看样例直接水过

#include
using namespace std;
typedef long long ll;
const int N = 1e6 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int main()
{
    int T;
    cin >> T;
    while(T --){
        int n;
        cin >> n;
        cout << n + 1 << endl;
    }
    return 0;
}

D二进制枚举

学到了一个新函数  __builtin_popcount(x)  计算x的二进制中有多少个1

#include
using namespace std;
typedef long long ll;
const int N = 2e4 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
ll l[10],r[10];
int main()
{
    int T;
    cin >> T;
    while(T --){
        int n;
        cin >> n;
        for(int i = 0;i < 7;i ++)
            cin >> l[i] >> r[i];
        int ans = 0;
        for(int i = 0;i < (1<<7);i ++){
            ll sl=0,sr=0;
            for(int j = 0;j < 7;j ++){
                if(i&(1<= sl && n <= sr)
                ans = max(ans,__builtin_popcount(i));
        }
        cout << ans << endl;
    }
    return 0;
}

E排个序

#include
using namespace std;
typedef long long ll;
const int N = 2e4 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
int p[N];
 
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&p[i]);
        sort(p+1,p+n+1);
        int res=k;
        for(int i=2;i<=n;i++)
            res+=(p[i]-p[i-1])*(p[i]-p[i-1]);
        printf("%d\n",res);
    }
    return 0;
}

H快速幂

#include
using namespace std;
typedef long long ll;
const int N = 2e4 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
ll quickpow(ll a,ll b)
{
    ll ans = 1;
    while(b){
        if(b&1)
            ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int main()
{
    int T;
    cin >> T;
    while(T --){
        int n;
        ll ans = 1;
        cin >> n;
        int a,b;
        for(int i = 1;i <= n;i ++){
            cin >> a >> b;
            ans = 1LL*ans*(quickpow(2,a)+quickpow(2,b)-2)%mod;
        }
        cout << ans << endl;
    }
    return 0;
}

I纯模拟

#include
using namespace std;
typedef long long ll;
const int N = 2e4 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
struct node
{
    int index;
    int data;
    char type;
}que[1005];
mapm;
int main()
{
    int t;
    char s[105];
    scanf("%d", &t);
    while (t--)
    {
        char shunxu[3];
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            que[i].index = i;
            scanf("%d", &que[i].data);
            scanf("%*s%s",&s);
            que[i].type = s[0];
        }
        scanf("%s", s);
        // 1-z 2-t 3-p
        m[s[0]] = 0;
        int flag = 1;
        for (int i = 1; i < strlen(s); i++)
        {
            if (s[i - 1] == ',')
                m[s[i]] = flag++;
        }
        sort(que, que + n, [](node q, node w)
        {
            if (q.type != w.type)
                return m[q.type] < m[w.type];
            return q.index < w.index;
        });
        for (int i = 0; i < n; i++)
            printf("%d%c", que[i].data, i != n - 1 ? ' ' : '\n');
    }
}

Jif判断

#include
using namespace std;
typedef long long ll;
const int N = 1e6 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int main()
{
    int T;
    cin >> T;
    while(T --){
        int a, b, x, y;
        cin >> a >> b >> x >> y;
        
        if(a < 350){
           cout << "You have not enough minerals." << endl;
           
        }else if(b < 250){
            cout << "You require more vespene gas." << endl;
        }else if(x+6>y){
            cout << "You must construct additional pylons." << endl;
        }else{
            cout << "Carrier has arrived." << endl;
        }
    }
    return 0;
}

K 以为题中说了fi小于i  所以序号大的一定以序号小的为前置  for扫一遍即可

#include
using namespace std;
const int MAXN=20005;
int a[MAXN],b[MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&a[i],&b[i]);
        for(int i=2;i<=n;i++)
        {
            int f;
            scanf("%d",&f);
            a[i]+=a[f],b[i]+=b[f];
        }
        for(int i=1;i<=q;i++)
        {
            int x;
            scanf("%d",&x);
            printf("%d %d\n",a[x],b[x]);
        }
    }
    return 0;
}

 

你可能感兴趣的:(牛客网,牛客比赛)