【noip2012】d2解题报告

t1:裸的扩展欧几里得

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lo;
lo a,b,x,y;
void exgcd(lo a,lo b,lo &x,lo &y)
{
    if(b==0)
    {
        x=1,y=0;
        return;
    }
    exgcd(b,a%b,x,y);
    lo p=x,q=y;
    x=y,y=p-a/b*q;
}
int main()
{
    scanf("%lld%lld",&a,&b);
    exgcd(a,b,x,y);
    if(x>0)
    {
        lo t=0;
        while(x-b*(t+1)>0)
            t++;
        cout<<x-b*t;
    }
    else
    {
        lo t=0;
        while(x+b*t<0)
            t++;
        cout<<x+b*t;
    }
}

t2:二分,之前的博客:http://www.cnblogs.com/Loi-dfkdsmbd/p/7770764.html

#include<iostream>
#include<cstring>
#include<cstdio>
#define ri register int
using namespace std;
typedef long long lo;
lo n,m,num[1000010],ne[1000010],l,r,ans;
struct in
{
    lo x,y,z;
}ter[1000010];
inline void re(lo &a)
{
    a=0;
    char b=getchar();
    while(b<'0'||b>'9')
        b=getchar();
    while(b>='0'&&b<='9')
        a=a*10+b-'0',b=getchar();
}
bool check(lo mid)
{
    memset(ne,0,sizeof(ne));
    lo r1=-1000000007,l1=1000000007;
    for(ri i=1;i<=mid;i++)
        ne[ter[i].y]+=ter[i].x,ne[ter[i].z+1]-=ter[i].x,l1=min(l1,ter[i].y),r1=max(r1,ter[i].z);
    for(ri i=l1;i<=r1;i++)
        ne[i]+=ne[i-1];
    for(ri i=l1;i<=r1;i++)
        if(ne[i]>num[i])
            return 1;
    return 0;
}
int main()
{
    re(n),re(m);
    for(ri i=1;i<=n;i++)
        re(num[i]);
    for(ri i=1;i<=m;i++)
    {
        re(ter[i].x),re(ter[i].y),re(ter[i].z);
        if(ter[i].y>ter[i].z)
            swap(ter[i].y,ter[i].z);
    }
    l=0,r=n;
    while(l<=r)
    {
        lo mid=l+r>>1;
        if(check(mid))
            ans=mid,r=mid-1;
        else
            l=mid+1;
    }
    if(ans!=0)
        cout<<-1<<'\n'<<ans;
    else
        cout<<0<<'\n';
}

t3:自己最后没来的及打出来,看dalao的吧:http://www.cnblogs.com/illyaillyasviel/p/7705893.html

posted @ 2017-11-10 07:03  那一抹落日的橙  阅读(181)  评论(0编辑  收藏  举报