码迷,mamicode.com
首页 > 其他好文 > 详细

xjtu暑期集训2016-7-9 题解

时间:2016-07-13 17:19:28      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

A. 郭铮鹏与国际象棋

Description
郭铮鹏正在学习国际象棋。他已经学会了王、车和象的移动方式。国际象棋规则如下:棋盘由8行8列共64个格子组成,一个格子的位置通过数对(r,c)来表示,其中r表示行数c表示列数。
车一次能在水平方向或竖直方向移动任意格.
象一次能在对角线方向移动任意格.
王一次能在任何方向移动一格(水平、竖直或对角线方向)
郭铮鹏掌握了车和王的移动方式,但对于象的移动,他还没有搞清楚。郭铮鹏正在考虑这样的一个问题。一个象从(r1,c1)移动到(r2,c2)的最小移动次数是多少?假定这个棋盘上只有这一个棋子。

注意无解的情况

#include<bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n) for(int i=1;i<=n;i++)
#define MEM(a) memset(a,0,sizeof(a));
int main(){
//  freopen("a.in.cpp","r",stdin);
    int n;
    cin>>n;
    For(i,n) {
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        if ((a+b)%2!=(c+d)%2) puts("0");
        else if (a-b==c-d||a+b==c+d) puts("1");
        else puts("2");
    }

    return 0;
}

B. 郭铮鹏与食堂买饭

郭铮鹏发现食堂每次买饭都要排很久的队。
现在有N个人在郭铮鹏前面排着队,每个人需要的打饭时间是一个数,郭铮鹏想知道,如果他可以任意改变每个人在队伍中的位置(但不能改变其所需要的打饭时间),那么所有在郭铮鹏前面的人的最少等待时间和。
0 < N < 1000

#include<bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n) for(int i=1;i<=n;i++)
#define MEM(a) memset(a,0,sizeof(a));
typedef long long ll;
int a[10000];
ll ans=0;
int main(){
//  freopen("b.in.cpp","r",stdin);
    int n;
    scanf("%d",&n);
    For(i,n) 
    scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    For(i,n) ans+=(ll)(n-i+1)*a[i];
    cout<<ans<<endl;
    return 0;
}

C. 郭铮鹏与烧情侣

郭铮鹏在的学校里常常弥漫着恋爱的酸臭味,郭铮鹏作为一个热爱和平的FFF成员想知道他至少需要多少桶汽油才能烧完所有的情侣。
假设学校是一个N * M的图(0 < N, M < 1000),每个点如果是1则代表有情侣,如果是0则代表没有情侣。情侣与情侣之间如果是相邻的(即上下左右的关系)那么这两对情侣可以认为是一个联通块。已知郭铮鹏每个联通块每个联通块地去烧情侣(给联通块泼汽油方便些嘛),并且当一个联通块的点数为P的时候,郭铮鹏需要P^2桶汽油。

注意爆栈的情况,用bfs,赛场有人用并查集我也是资磁的((‵□′))

#include<bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++) 
#define MEM(a) memset(a,0,sizeof(a));
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define INF (2139062143)
#define fi first
#define se second
typedef long long ll;
int n,m,t;
int a[4000][4000],b[4000][4000];
queue<pi > q;
void dfs(int X,int Y){
    q.push(mp(X,Y));
    b[X][Y]=1;
    while(!q.empty()) {
        int x=q.front().fi,y=q.front().se;
        q.pop();
        ++t; 
        if (a[x-1][y]&&!b[x-1][y]) q.push(mp(x-1,y)),b[x-1][y]=1;
        if (a[x+1][y]&&!b[x+1][y]) q.push(mp(x+1,y)),b[x+1][y]=1;
        if (a[x][y-1]&&!b[x][y-1]) q.push(mp(x,y-1)),b[x][y-1]=1;
        if (a[x][y+1]&&!b[x][y+1]) q.push(mp(x,y+1)),b[x][y+1]=1;
    }
}
int main(){
//  freopen("c.in.cpp","r",stdin);
    cin>>n>>m;
    MEM(b) MEM(a)
    ll ans=0;
    For(i,n) For(j,m) scanf("%d",&a[i][j]);
    For(i,n) For(j,m) if (a[i][j]&&!b[i][j]) {
        t=0;
        dfs(i,j);
        ans+=(ll)t*t;
    //  cout<<t<<endl;
    }
    cout<<ans<<endl;

    return 0;
}

D. 郭铮鹏与线性代数

Description
郭铮鹏正在学习线性代数。有一天,他碰到了这样一道题目,给定平面上4 个不同的点,问
这四个点里取点所连成的直线中是否存在两条直线垂直?由于郭铮鹏已经快把高中数学忘
光了,他不得不求助于你。
Input
输入第一行为一个整数n, 1≤n≤20000
后面n 行,每行包含八个数x1,y1,x2,y2,x3,y3,x4,y4,分别表示四个点的坐标,保证这些数的绝
对值小于10000
Output
输出n 行,对于每一组输入数据,输出”Yes”表示存在,”No”表示不存在。
Sample Input
2
0 0 1 0 0 2 1 2
0 0 1 1 2 2 3 3
Sample Output
Yes
No

出数据的人用了小数QAQ

#include<bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++) 
double x[4],y[4];
int check() {
    Rep(i,4) Fork(j,i+1,3) Rep(k,4) Fork(l,k+1,3)
        if (fabs(((x[i]-x[j])*(x[k]-x[l])+(y[i]-y[j])*(y[k]-y[l])))<1e-9) return 1;
    return 0;
}
int main(){
//  freopen("d.in.cpp","r",stdin);

    int n;
    cin>>n;
    while(n--) {
        Rep(i,4) scanf("%lf%lf",&x[i],&y[i]);
        if (check()) puts("Yes"); else puts("No");
    }


    return 0;
}

E. 郭铮鹏与老司机

郭铮鹏听说有一种叫老司机的人非常厉害,于是他现在放弃计算机科学做起了老司机。
有一天有N 个人要郭铮鹏这个老司机带带他/她,郭铮鹏的车最终只能再装入体重和不超过M 的人们。现在已知每个人的体重,问在体重和不超过M的情况下,郭铮鹏能最多带的体重和。
输入:
一共两行
第一个行两个数 N, M(0

#include<bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n) for(int i=1;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)

#define Fork(i,k,n) for(int i=k;i<=n;i++) 
#define MEM(a) memset(a,0,sizeof(a));
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define INF (2139062143)
#define fi first
#define se second
typedef long long ll;
int n,m,t;
int f[2000];
int main(){
//  freopen("e.in.cpp","r",stdin);
    cin>>n>>m;
    MEM(f) f[0]=1;
    For(j,n) {
        int w;cin>>w;
        ForkD(i,w,m) {
            f[i]|=f[i-w];
        }
    }
    int ans=m;
    while(!f[ans]) --ans;
    printf("%d\n",ans);

    return 0;
}

F. GZP and Public Display of Affection

As having being alone for more than ten thousand years, he is likely to be hurt when some couples showing their love to each other in public occasions.
It is known that, if a couple which had shown their love to GZP has been proved to be brothers and sisters, the degree of hurt of GZP will decrease; and if GZP is hurt by a couple for more than one time, some degrees of hurt will increase.
GZP want to know the sum of hurt degrees in some periods of time.
There are two number in first line, N and M.
In the next line there are N numbers, which are the degrees of hurt.
There are M lines following, and for each line, there are two kinds of operations. The first is to ask the sum of hurt from the i-th day to the j-th (include i and j). The second is to change the hurt degree to X in some particular day.
The first line : N,M (1 <= N,M <= 100000)
The second line : ai (abs(ai) <= 1000)
The following M lines:
For each line : opt,l,r;
if Opt == 1, output the sum of [l,r]
if Opt == 2, change the L-th number to R
Sample Input:
5 3
1 2 3 4 5
1 2 4
2 3 2
1 2 4
Sample Output:
9
8

树状数组单点修改,区间求和。
有人暴力卡常过,如果这样的话记得sum[l,r]=tot-sum[1,l-1]-sum[r+1,n]

#include<bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n) for(int i=1;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)

#define Fork(i,k,n) for(int i=k;i<=n;i++) 
#define MEM(a) memset(a,0,sizeof(a));
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define INF (2139062143)
#define fi first
#define se second
#define MAXN (100000*4)
typedef long long ll;
int n,m,t;
double b[MAXN]={0};
int lowbit(int x){return x&(-x);}
int add(int x,double v) {
    for(int i=x;i<=n;i+=lowbit(i)) b[i]+=v;
}
double qur(int x) {
    double ans=0;
    for(int i=x;i;i-=lowbit(i)) ans+=b[i];
    return ans;
}
int main(){
//  freopen("f.in.cpp","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF) {
        For(i,n) b[i]=0;
        For(i,n) {
            double p;
            scanf("%lf",&p);
            add(i,p);
        }
        For(i,m) {
            int p,l;
            scanf("%d%d",&p,&l);
            if (p==1) {
                int r;
                scanf("%d",&r);
                printf("%.0lf\n",qur(r)-qur(l-1));
            } else {
                double r;
                scanf("%lf",&r);
                double t=qur(l)-qur(l-1);
                add(l,r-t);
            }
        }
    }

    return 0;
}

G. GZP, Bureaucracy and Girlfriend

GZP is a student in Na’ix College of Ompxtqx Ctxenongly , which is one of the third batch of
undergraduate. One day, GZP wants to set up a student organization in order to know more girls,
as he thinks that it may increase the possibility of finding his girlfriend.
Then he want to know some regulations about setting up the organization. He searched all the
pages of the college website. It is interesting that he can not find any information about it, or it just
returns a “404 Not Found” by Windows IIS.
Then he cut several classes to find somebody to tell how he should do. Fortunately, he finally find
the person named NjcpuRAcb, who is in charge of student organizations with sacrifice of failing
many courses as there is a rule that every student should not cut more than three classes of one
course except for dead students. But NjcpuRAcb told he that he should get to LwocHiobKsw,
XwocKiQvi and QowKiQvi to get permissions in advance. So GZP has to get to LwocHiobKsw,
XwocKiQvi and QowKiQvi to ask for permissions. Unfortunately, each of three asks for some
permissions in advance from other people as well.
GZP wonders whether he could increase the poor possibility.
Obviously, if there exists a circle in the permission chain, GZP will be forever alone.
Input:
There are several test cases in this Problem. Each of them begins with a integer N (N <= 1000). Then
N lines follow, each line begins with a name, which require permissions from the left names of the
same line. (Some teachers may be kind enough that he/she does not require any other
permissions.)
It is guaranteed that there will be no same names in one line and no lines begin with same name
in one test case.
Names in a line are seprated by spaces, and it is guaranteed that there are only english characters
in names.
Output:
If GZP can set up the organization, just output “Wish you could see the first snow with your love
this year.” in a line; if he can not, just output “Forever Single Dog!” in a line.
Sample Input:
6
NjcpuRAcb LwocHiobKsw XwocKiQvi QowKiQvi
LwocHiobKsw EwiMwZs
XwocKiQvi EwiMwZs NvocUiobuMobu
QowKiQvi NvocUiobuMobu
NvocUiobuMobu EwiMwZs
EwiMwZs NjcpuRAcb
2
Hobo Eddie
Eddie
Sample Output:
Forever Single Dog!
Wish you could see the first snow with your love this year.

0 AC.
虽然是拓扑排序,但是TLE到不要不要的
数据里基本提到过没出现的所有情况(比如很长的字符串,完全图,还有空行,还有还没读到最后一行就EOF的。。)

,考验hash和卡常的时候到了

#include<bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n) for(int i=1;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--) 
#define Fork(i,k,n) for(int i=k;i<=n;i++) 
#define MEM(a) memset(a,0,sizeof(a));
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define INF (2139062143)
#define fi first
#define se second
#define MAXN (100000)
typedef long long ll;
typedef unsigned long long ull;
int n;
char s[10000000];
int id=0;
ull calc(bool &b,bool &b2) { //b 是否回车 b2 是否读入字符串 
    char c;
    while((c=getchar())==‘ ‘);
    if (c==‘\n‘) {b=1;b2=0;return 0; } 
    b2=1;
    ull t=c;
    while(1) {
        c=getchar();
        if (c==‘ ‘||c==‘\n‘) break;
        t=t*10000007+c;
    }
    b=c==‘\n‘; 
    return t;
}
map<ull,int> h;
map<ull,int>::iterator it;
map<ull,string> h2;

int get(ull t) {
    while(1) {
        it=h.find(t);
        if (it==h.end())  {
            h[t]=++id;
            return id;
        }
        else {
            return it->se;  
        }
    }
}
vector<int> edge[MAXN];
int deg[MAXN];
queue<int> q;
bool topsort() {
    int n=id,t=0;
    For(i,n) if (!deg[i]) q.push(i);
    while(!q.empty()) {
        int x=q.front(); q.pop();
        int sz=edge[x].size();
        Rep(i,sz) {
            int v=edge[x][i];
            deg[v]--;
            if (!deg[v]) q.push(v);
        }       
        t++;
    }
    if (t==n) return 1;
    return 0;
}
char p[10000000];
string p2;
int main(){
//  freopen("7621/3.in","r",stdin);
//  freopen("G.out","w",stdout);

    while(scanf("%d",&n)!=EOF) {
        while(getchar()!=‘\n‘);
        MEM(deg) h.clear(); h2.clear(); id=0;
        For(i,n) {
            bool b,b2;
            calc(b,b2);
            int len=strlen(s);

            int lenp,nowi=0;
            {
                lenp=0;
                while(nowi<len&&s[nowi]==‘ ‘) nowi++;
                while(nowi<len&&s[nowi]!=‘ ‘) p[lenp++]=s[nowi],nowi++;
                p[lenp]=0;
            }
            int u=get(p,lenp);
            while(nowi<len) {
                {
                    lenp=0;
                    while(nowi<len&&s[nowi]==‘ ‘) nowi++;
                    while(nowi<len&&s[nowi]!=‘ ‘) p[lenp++]=s[nowi],nowi++;
                    p[lenp]=0; 
                }
                if (!lenp) continue; 
                int v=get(p,lenp);
                edge[u].pb(v);
                deg[v]++;   
            }
        }
        if (topsort()) puts("Wish you could see the first snow with your love this year."); 
        else puts("Forever Single Dog!");   

        For(i,id) edge[i].clear();
    }
    return 0;
}

H. GZP and Dating

Description
GZP wants to find a good girlfriend as soon as possible. After setting up a student union with a lot of girls, GZP invites several girls to dinner every night. GZP has a budget about the amount of money to pay every night, on the i-th day, he will spend R[i] yuan, M days in total. GZP has no money with him but N bank cards and the i-th has L[i] yuan. There’s a rule in the restaurant: GZP has to pay the money of a meal with only one bank card. Here comes the problem: how many meals GZP can afford at most? Let’s assume GZP will not spend any money except for girls.
Input
A integer N and M on the first line, 1 ≤ N ≤ 50
The next N lines contains L[i] respectively, 1 ≤ Li ≤ 10^9
The next line is M, 1 ≤ M ≤ 1023
The next M lines contains R[i] respectively, 1 ≤ Ri ≤ 128
Output
One integer, the number of meals GZP can afford
Sample Input
4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30
Sample Output
7
Hint
50–>15+16+19 or 15+16+17 or 15+17+18
40–>19+21
25–>25
30–>30

原题USACO fence8
http://www.nocow.cn/index.php/Translate:USACO/fence8

广度多维背包问题:虽然正解是暴搜+剪枝

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<‘ ‘; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<‘ ‘;                        cout<<a[i][m]<<endl;                         } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch==‘-‘) f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-‘0‘; ch=getchar();}
    return x*f;
} 
#define MAXN (50+10)
#define MAXM (2000+10)
int n,m,mi;
ll L[MAXN],S=0,Sr[MAXM],r[MAXM];
int dfs(int x,int pos,ll spa) {  // now deal with item_x 
    if (!x) return 1;
    if (S-Sr[mi]<spa) return 0; 
    Fork(i,pos,n) {
        if (L[i]>=r[x]) {
            L[i]-=r[x]; 
            if (L[i]<r[1]) spa+=L[i]; 
            bool fl=dfs(x-1, (r[x]==r[x-1] )? i: 1 , spa);
            if (L[i]<r[1]) spa-=L[i]; 
            L[i]+=r[x];
            if (fl) return 1; 
        } 
    } 
    return 0;
}
int main()
{
//  freopen("H.in","r",stdin);
 // freopen(".out","w",stdout);


    n=read(); 
    For(i,n) cin>>L[i],S+=L[i]; //bag 
    m=read();
    For(i,m) cin>>r[i]; //item
    sort(L+1,L+1+n,greater<ll>());
    r[0]=0;
    sort(r+1,r+1+m);
    Sr[0]=0;
    For(i,m) Sr[i]=Sr[i-1] + r[i];

    while(Sr[m]>S) --m;
    while(r[m]>L[1]) --m;

    int le=0,ri=m,ans=0;
    while(le<=ri) {
        mi=le+ri>>1; // now r_i from 1..m
        if (dfs(mi,1,0)) ans=mi,le=mi+1;
        else ri=mi-1;
    }
    cout<<ans<<endl;
    return 0;
}

xjtu暑期集训2016-7-9 题解

标签:

原文地址:http://blog.csdn.net/nike0good/article/details/51887253

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!