夏夜、

心若平似镜、何题不AC。

2014浙大校赛

ZOJ 3767 Elevator

求和,签到题

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("in.txt","r",stdin);
    //freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

int main()
{
    int n,m;
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        scanf("%d%d",&n,&m);
        int res=0;
        for(int i=0;i<n;i++)
        {
            int x;scanf("%d",&x);
            res+=x;
        }
        printf("%s\n",res<=m?"Safe":"Warning");
    }
    return 0;
}
View Code

ZOJ 3775 ?(>_o)!

题意中一大堆无用信息,其实就是判断源码和输出是否一样。简单字符串处理,string很方便

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("in.txt","r",stdin);
    //freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

int main()
{
    int t;
    cin>>t;
    getchar();
    for(int ca=1;ca<=t;ca++)
    {
        string s1;
        getline(cin,s1);
        string s2;
        for(int i=0;i<s1.length();i++)
        {
            if(s1[i]=='_')
                s2+=s1;
            if(s1[i]=='!')
                s2+="Hello, world!";
        }
       // cout<<s1<<endl<<s2<<endl;
        cout<<(s1==s2?"Yes":"No")<<endl;
    }
    return 0;
}
View Code

ZOJ 3770 Ranking System

按比例分等级,排序

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("in.txt","r",stdin);
    //freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

struct number
{
    int id;
    string date;
    int score;
    int order;
    int lv;
    bool operator < (const number &ant) const
    {
        if(score!=ant.score)return score>ant.score;
        else if(date!=ant.date) return date<ant.date;
        else return id<ant.id;
    }
}da[2005];

bool cmp(const number &a,const number &b)
{
    return a.order<b.order;
}
int main()
{
    int t;
    cin>>t;
    for(int ca=1;ca<=t;ca++)
    {
        int n;cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>da[i].id>>da[i].date>>da[i].score;
            da[i].order=i;
        }

        sort(da,da+n);
        int num=0;
        while(num<n&&da[num].score>0)num++;

        int pre=0;
        int x=(int)(num*0.03);
        for(int i=pre;i<pre+x;i++)
            da[i].lv=6;
        pre=pre+x;

        x=(int)(num*0.07);
        for(int i=pre;i<pre+x;i++)
            da[i].lv=5;
        pre=pre+x;

        x=(int)(num*0.2);
        for(int i=pre;i<pre+x;i++)
            da[i].lv=4;
        pre=pre+x;

        x=(int)(num*0.3);
        for(int i=pre;i<pre+x;i++)
            da[i].lv=3;
        pre=pre+x;

        for(;pre<n&&da[pre].score>0;pre++)
            da[pre].lv=2;

        for(;pre<n;pre++)
            da[pre].lv=1;

        sort(da,da+n,cmp);
        for(int i=0;i<n;i++)
            printf("LV%d\n",da[i].lv);
    }
    return 0;
}
View Code

ZOJ 3768 Continuous Login

 将n分解为尽量少个前缀和,搜索,暴力出奇迹,我用的是迭代加深

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("in.txt","r",stdin);
    //freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

const int maxn=40000;
int f[maxn];
int ans[maxn];

int cal(int n)
{
    int k=(int)(sqrt(n*2)+1);
    while(f[k]>n)k--;
    return k;
}

int dfs(int d,int n,int k)
{
    if(k==1)
    {
        ans[k]=cal(n);
        if(n==f[cal(n)])return true;
    }else
    {
        for(int x=min(d,cal(n));x>=1;x--)
        {
            ans[k]=x;
            if(dfs(x,n-f[x],k-1))return true;
        }
    }
    return 0;
}
int main()
{
    for(int i=1;i<maxn;i++)
        f[i]=i*(i+1)/2;
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        int n;
        scanf("%d",&n);

        int res;
        for(int i=1;;i++)
        {
            if(dfs(cal(n),n,i))
            {
                res=i;break;
            }
        }
        for(int i=1;i<=res;i++)
            printf("%d%c",ans[i],i==res?'\n':' ');
    }
    return 0;
}
View Code

ZOJ 3772 Calculate the Function

做法1:线段树维护区间矩阵乘积,注意是左乘还是右乘

做法2:预处理出全部前缀矩阵乘积和全部前缀逆矩阵乘积(就跟区间和sum(b)-sum(a-1)一样)

线段树:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("in.txt","r",stdin);
    //freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

const int mod=1000000007;

struct Matrix
{
    ll a[2][2];
    Matrix(){memset(a,0,sizeof(a));}
    void set(ll A)
    {
        a[0][0]=a[1][0]=1;
        a[0][1]=A;
        a[1][1]=0;
    }
    void setI()
    {
        a[0][0]=a[1][1]=1;
        a[0][1]=a[1][0]=0;
    }
};
Matrix operator * (const Matrix &a,const Matrix &b)
{
    Matrix res;
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                res.a[i][j]=(res.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
    return res;
}

const int  maxn=100005;
ll A[maxn];
Matrix v[maxn<<2];
void PushUp(int idx)
{
    v[idx]=v[idx<<1|1]*v[idx<<1];
}
void build(int idx,int l,int r)
{
    if(l==r)
    {
        v[idx].set(A[r]);
    }else
    {
        int mid=(r+l)>>1;
        build(lson); build(rson);
        PushUp(idx);
    }
}
Matrix query(int idx,int l,int r,int tl,int tr)
{
    if(tl<=l&&tr>=r)
        return v[idx];
    Matrix res;
    res.setI();
    int mid=(r+l)>>1;
    if(tl<=mid) res=res*query(lson,tl,tr);
    if(tr>mid)  res=query(rson,tl,tr)*res;
    return res;
}
ll cal(const Matrix &x,int a,int b)
{
    return (x.a[0][0]*a+x.a[0][1]*b)%mod;
}
int main()
{
    int t; scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        int n,q; scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++) scanf("%lld",&A[i]);
        build(1,1,n);

        for(int Q=1;Q<=q;Q++)
        {
            int l,r; scanf("%d%d",&l,&r);
            ll res=0;
            if(r-l<2) res=A[r];
            else
            {
                Matrix x=query(1,1,n,l+2,r);
                res=cal(x,A[l+1],A[l]);
            }
            printf("%lld\n",res);
        }
    }
    return 0;
}
View Code

ZOJ 3769 Diablo III

背包,先把特殊的装备处理了,把任意一对Finger当成一件装备还有把Weapon 和Shield 的任意组合当成Two-Handed

然后就是普通的背包了,dp[i][j]表示装备前i类装备防御力为j的最大攻击力(j>=m的情况都当成是m)

需要优化:把攻击力和防御力都低的舍掉。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("in.txt","r",stdin);
    //freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

struct attri
{
    int a,b;
};
map<string,int> id;
int ecnt;
int ID(string s)
{
    if(id.find(s)==id.end())
        id[s]=++ecnt;
    return id[s];
}

vector<attri> da[15];
void deal()
{
    //combine weapon and shield
    int a=ID(string("Weapon"));
    da[a].push_back((attri){0,0});

    int b=ID(string("Shield"));
    da[b].push_back((attri){0,0});

    int c=ID(string("Two-Handed"));
    for(int i=0;i<da[a].size();i++)
        for(int j=0;j<da[b].size();j++)
            da[c].push_back((attri){
                            da[a][i].a+da[b][j].a,
                            da[a][i].b+da[b][j].b
                            });
    da[a].clear();
    da[b].clear();

    //combine two finger
    int d=ID(string("Finger"));
    da[d].push_back((attri){0,0});

    vector<attri> buf;
    for(int i=0;i<da[d].size();i++)
        for(int j=i+1;j<da[d].size();j++)
            buf.push_back((attri){
                          da[d][i].a+da[d][j].a,
                          da[d][i].b+da[d][j].b});
    da[d]=buf;
    for(int i=1;i<=13;i++)
        da[i].push_back((attri){0,0});
}


int dp[15][50005];

int main()
{
    std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    for(int ca=1;ca<=t;ca++)
    {
        int n,m;
        cin>>n>>m;
        ecnt=0;
        id.clear();
        for(int i=1;i<=13;i++)
            da[i].clear();

        for(int i=1;i<=n;i++)
        {
            string s;
            int a,b;
            cin>>s>>a>>b;

            int id=ID(s);

            da[id].push_back((attri){a,b});
        }

        deal();

        memset(dp,-1,sizeof(dp));
        dp[0][0]=0;
//        for(int i=1;i<=13;i++)
//        {
//            cout<<"ID:"<<i<<endl;
//            for(int j=0;j<da[i].size();j++)
//                cout<<da[i][j].a<<' '<<da[i][j].b<<endl;
//
//        }
        for(int i=1;i<=13;i++)
        {
            int maxa=0,maxb=0;
            for(int j=0;j<da[i].size();j++)
            {
                if(da[i][j].a<maxa&&da[i][j].b<maxb)continue;
                if(da[i][j].a>maxa&&da[i][j].b>maxb)
                {
                    maxa=da[i][j].a;maxb=da[i][j].b;
                }
                for(int d=0;d<=m;d++)if(dp[i-1][d]!=-1)
                {
                    int b=d+da[i][j].b;
                    if(b>=m)b=m;
                    dp[i][b]=max(dp[i][b],dp[i-1][d]+da[i][j].a);
                }
            }
        }

        cout<<dp[13][m]<<endl;
    }
    return 0;
}
View Code

 目前会做的只有这6题了。。

posted on 2014-04-07 10:51  BMan、  阅读(292)  评论(0编辑  收藏  举报

导航