刷题#R4

60 篇文章 0 订阅
26 篇文章 0 订阅

题目链接
T1 模拟即可,但是要仔细一点。
T2 素数筛,前缀和,简单二分就可以过。
T3 30分floyed暴力。
T1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100009;
char s[N];
int n,sl[N],sr[N],ans;
int main()
{
    freopen("shower.in","r",stdin);
    freopen("shower.out","w",stdout);
    gets(s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;i++)
    {
        sl[i]=sl[i-1],sr[i]=sr[i-1];
        if(s[i]=='(') sl[i]++;
        if(s[i]==')') sr[i]++;
        if(sr[i]>sl[i])
        {
            ans++;
            sr[i]--;
            sl[i]++;
        }
    } 
    ans+=(sl[n]-sr[n])/2;
    printf("%d",ans);
    return 0;
}

T2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
#define LL long long
using namespace std;
const int N=1000009;
int T,k,prime[N],cnt;
bool np[N];
LL sum[N],n;
void pre()
{
    for(int i=2;i<=N-5;i++)
    {
        if(!np[i]) prime[++cnt]=i;

        for(int j=1;j<=cnt&&1ll*i*prime[j]<=N-5;j++)
        {
            np[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        } 
    }
    for(int i=1;i<=cnt;i++)
      sum[i]=sum[i-1]+prime[i];
    //cout<<sum[cnt];
}
int main()
{

    freopen("diary.in","r",stdin);
    freopen("diary.out","w",stdout);
    pre();
    scanf("%d",&T);
    int f;
    while(T--)
    {
        f=0;
        scanf("%lld%d",&n,&k);
        for(int i=k;i<=cnt;i++)
        {
            if(sum[i+1]-sum[i-k+1]<=n) continue;
            if(sum[i]-sum[i-k]<=n)
            {
                printf("%lld\n",sum[i]-sum[i-k]); 
                f=1;
            }
            break;
        }
        if(!f) printf("-1\n");
    }
    //cout<<endl<<"time:"<<clock()/1000.0<<endl;//0.30~0.50
    return 0;
}

T3

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>

using namespace std;

const int mo=1000000007;
const int maxn=100;

int n,id1[maxn],id2[maxn],l[maxn],res[maxn];

long long num1[maxn],num2[maxn],size[maxn];

struct rec
{
    int p;
    long long p1,p2;
    rec(){}
    rec(int a,long long b,long long c)
    {
        p=a;
        if (b<c) p1=b,p2=c;
        else p1=c,p2=b;
    }
    bool operator<(const rec &a)const
    {
        if (p!=a.p) return p<a.p;
        if (p1!=a.p1) return p1<a.p1;
        return p2<a.p2;
    }
};

map< pair<int,long long > ,int > ma;

map<rec,int> ma2;

int solve(int p,long long p1,long long p2)
{
    if (!p) return 0;
    if (p1==p2) return 0;
    rec x=rec(p,p1,p2);
    if (ma2.count(x)) return ma2[x];
    if (p1<size[id1[p]])
    {
        if (p2<size[id1[p]]) ma2[x]=solve(id1[p],p1,p2);
        else ma2[x]=((long long)solve(id1[p],num1[p],p1)+solve(id2[p],num2[p],p2-size[id1[p]])+l[p])%mo;
    }
    else
    {
        if (p2<size[id1[p]]) ma2[x]=((long long)solve(id1[p],num1[p],p2)+solve(id2[p],num2[p],p1-size[id1[p]])+l[p])%mo;
        else ma2[x]=solve(id2[p],p1-size[id1[p]],p2-size[id1[p]]);
    }
    return ma2[x];
}

int solve(int p,long long n)
{
    if (p==0) return 0;
    pair<int,long long> px;
    px=make_pair(p,n);
    if (ma.count(make_pair(p,n))) return ma[px];
    if (n<size[id1[p]]) ma[px]=(((long long)solve(id1[p],num1[p],n)+l[p])*(size[id2[p]]%mo)%mo+solve(id2[p],num2[p])+solve(id1[p],n))%mo;
    else ma[px]=(((long long)solve(id2[p],num2[p],n-size[id1[p]])+l[p])*(size[id1[p]]%mo)%mo+solve(id1[p],num1[p])+solve(id2[p],n-size[id1[p]]))%mo;
    return ma[px];
}

int main()
{
    freopen("cloth.in","r",stdin);
    freopen("cloth.out","w",stdout);

    while (~scanf("%d",&n))
    {
        ma.clear();
        ma2.clear();
        for (int a=1;a<=n;a++)
            scanf("%d%d%I64d%I64d%d",&id1[a],&id2[a],&num1[a],&num2[a],&l[a]);
        size[0]=1;
        for (int a=1;a<=n;a++)
            size[a]=size[id1[a]]+size[id2[a]];
        for (int a=1;a<=n;a++)
            res[a]=((long long)solve(id1[a],num1[a])*(size[id2[a]]%mo)%mo+(long long)(size[id1[a]]%mo)*(size[id2[a]]%mo)%mo*l[a]%mo+(long long)solve(id2[a],num2[a])*(size[id1[a]]%mo)%mo+res[id1[a]]+res[id2[a]])%mo;
        for (int a=1;a<=n;a++)
            printf("%d\n",res[a]);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值