BUPT 2014新生暑假个人排位赛04

来源:互联网 发布:淘宝快递一直显示揽件 编辑:程序博客网 时间:2024/05/16 09:55

BOJ 437 大家一起点外卖

基数排序
#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath> using namespace std;  template<class T>inline bool read(T &n){    T x = 0, tmp = 1; char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;} //----------------------------------------------------------------------- const int MAXN=2000010;const int INF=0x3f3f3f3f;int n,m;int a[MAXN]; int main(){    int T,maxx=-INF,minn=INF;    read(T);    while(T--)    {        read(n);read(m);        minn=INF;        maxx=-INF;        int ans1,ans2;        memset(a,0,sizeof(a));        for(int i=0;i<n;i++)        {            int temp;            read(temp);            a[temp]++;            if(temp>maxx)                maxx=temp;        }        if(maxx<m/2)        {            puts("Sad");continue;        }        else if(maxx==m/2)        {            if(m&1==0&&a[m/2]>1)//even            {                printf("%d %d\n",m/2,m/2);continue;            }            else if(m&1==1)            {                puts("Sad");continue;            }        }        for(int i=m/2;i>=1;i--)        {            if(a[i]&&a[m-i])            {                if(i==m-i)                {                    if(a[i]>2)                    {                        minn=m-2*i;                        ans1=i,ans2=m-i;                        break;                    }                    else continue;                }                else                {                    minn=m-2*i;                    ans1=i,ans2=m-i;                    break;                }             }        }        if(minn==INF)            puts("Sad");        else            printf("%d %d\n",ans1,ans2);    }}

BOJ 438 田田的公司

并查集
<span style="font-size:12px;">#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath> using namespace std;  template<class T>inline bool read(T &n){    T x = 0, tmp = 1; char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;} //-----------------------------------------------------------------------const int MAXN=100010;typedef long long ll;ll a[MAXN],fa[MAXN];int n,q; int find_fa(int x){    if(fa[x]==x)        return x;    else        return fa[x]=find_fa(fa[x]);} void Merge(int x,int y){    int xx=find_fa(x);    int yy=find_fa(y);    if(xx!=yy)    {        if(a[yy]>a[xx])        {            fa[xx]=yy;            a[yy]+=a[xx];        }        else        {            fa[yy]=xx;            a[xx]+=a[yy];        }    }}  int main(){    int T;    read(T);    while(T--)    {        read(n);read(q);        for(int i=1;i<=n;i++)        {            read(a[i]);            fa[i]=i;        }        while(q--)        {            int t,x,y;            read(t);            if(t==1)            {                read(x);read(y);                Merge(x,y);//<<a[x]<<" "<<a[y]<<endl;            }            else            {                read(x);                printf("%lld\n",a[find_fa(x)]);            }        }    }}</span>

BOJ 439 崔逗逗的难题

#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath> using namespace std;  template<class T>inline bool read(T &n){    T x = 0, tmp = 1; char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;} //----------------------------------------------------------------------- const double PI=acos(-1.0);const double sqr3=sqrt(3.0);const double m3=1-PI/6-sqr3/4;const double m1=1+PI/3-sqr3;const double m2=PI/12-1+sqr3/2; int main(){    double r;    while(~scanf("%lf",&r))    {        double k=2.0-sqr3;        double a=r*r*k+r*r*(PI/3.0-1),b=(PI-2.0)*r*r-2.0*a,c=r*r-a-b;        printf("%.6f %.6f %.6f\n",a,b,c);    }    return 0;}


BOJ 435 崔逗逗给你信心



#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath> using namespace std; template<class T>inline bool read(T &n){    T x = 0, tmp = 1; char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;}//--------------------------------------------------------------------------- typedef long long ll;const int MOD=1000000009;int c[100],tot=0,flag=0,even=1;ll f[100]={1,1,2},sum[100]={0,1,2,4};  int solve(ll a){    if(!a)        return 0;    int temp=solve(a>>(ll)1);    if(flag)    {        c[tot]=even;        even=!even;        return c[tot++];    }    if(temp&&(a%2)==1)        c[tot]=0,flag=1,even=1;    else        c[tot]=(a%2);    return c[tot++];}  int main(){    for(int i=3;i<100;i++)    {        f[i]=(f[i-1]+f[i-2])%MOD;        sum[i+1]=(sum[i]+f[i])%MOD;    }  /*  for(int i=0;i<10;i++)        cout<<f[i]<<" ";    cout<<endl;    for(int i=0;i<10;i++)        cout<<sum[i]<<" ";    cout<<endl;*/    ll n;    while(read(n))    {        memset(c,0,sizeof(c));tot=0;flag=0;        solve(n);        ll ans=1;        for(int i=0;i<tot;i++)            if(c[i])                ans=(1+ans+sum[tot-i-1])%MOD;        printf("%lld\n",ans);    }    return 0;}


BOJ 434 焦级长搭积木

dp公式简单,用递推的时间会浪费更多,记忆话搜索会比较好一些;
字典序那边有点难理解,左子树的字典序小于右字数的字典序,父亲节点的方案数是两个子树的和,两个子树之间没有关系。当K大于左子树的方案数的时候,必定是属于右子树的范围,则必须减去左子树的方案数才是右子树中字典序的排序数量。

</pre></div><pre name="code" class="cpp">#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#define eps 1e-9#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;typedef long double ld;typedef pair<ll, ll> pll;typedef complex<ld> point;typedef pair<int, int> pii;typedef pair<pii, int> piii;const int maxn = 3 * 1e5;template<class T>inline bool read(T &n){    T x = 0, tmp = 1; char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;}//--------------------------------------------------------ll dp[550][61][11];  ll dfs(int m,int h, int n)  {      ll &t=dp[n][h][m];      if(t>=0)return t;  t=0;    if(n<=0)return t=0;      if(h==1){          if(n==m)return t=1;          elsereturn t=0;      }      if(m<10&&n>m)t+=dfs(m+1,h-1,n-m);      if(m>1&&n>m)t+=dfs(m-1,h-1,n-m);      return t;  } int ans[111],tot=0;bool find_way(int m,int h,int n,ll k){if(k==0&&h==1&&n==m){ans[tot++]=m;return true;}if(n<=0||h==0)return false;if(m==1&&find_way(2,h-1,n-1,k)){ans[tot++]=1;return true;}if(m==10&&find_way(9,h-1,n-10,k)){ans[tot++]=10;return true;}if(m>1&&k<dp[n-m][h-1][m-1]&&find_way(m-1,h-1,n-m,k)){ans[tot++]=m;return true;}if(m<10&&k>=dp[n-m][h-1][m-1]&&find_way(m+1,h-1,n-m,k-dp[n-m][h-1][m-1])){ans[tot++]=m;return true;}return false;}int main(){int h,m,n;while(read(n)&&read(h)&&read(m)){memset(dp,-1,sizeof(dp));printf("%lld\n",dfs(m,h,n));ll k;while(read(k)&&k!=-1){tot=0;find_way(m,h,n,k-1);for(int i=tot-1;i>=0;i--)printf("%d%c",ans[i],i==0?'\n':' ');}} return 0;}


0 0