小比赛(CodeForces 560A,UVA 11040,CodeForces 550B,HDU 1856,UVA 1644,CodeForces 560B,HDU 3405,UVA 10820)

比赛链接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88591#overview

密码:

431


1.Currency System in Geraldion

题目链接:

http://codeforces.com/contest/560/problem/A

解题思路:

Just check is the 1 in the set.

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
    int n;
    while(~scanf("%d",&n)){
        int x,flag = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d",&x);
            if(x == 1)
                flag = 1;
        }
        if(flag)
            printf("-1\n");
        else
            printf("1\n");
    }
    return 0;
}

2.Add bricks in the wall

题目链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1981

解题思路:

模拟杨辉三角即可。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int a[10][10];

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n = 9;
        memset(a,0,sizeof(a));
        for(int i = 1; i <= n; i+=2)
            for(int j = 1; j <= i ;j+=2)
               scanf("%d",&a[i][j]);
        for(int i = 1; i <= n-2; i+=2)
            for(int j = 1; j <= i ;j+=2)
                a[i+2][j+1] = (a[i][j]-a[i+2][j]-a[i+2][j+2])/2;
        for(int i = 8; i >= 2; i--)
            for(int j = 1; j <= i; j++)
                a[i][j] = a[i+1][j]+a[i+1][j+1];
        for(int i = 1; i <= 9; i++){
            for(int j = 1; j < i; j++)
                printf("%d ",a[i][j]);
            printf("%d\n",a[i][i]);
        }
    }
    return 0;
}


3.Preparing Olympiad

题目链接:

http://codeforces.com/problemset/problem/550/B

解题思路:

Because of the low constraints, this problem can be solved by complete search over all problem sets (there are 2n of them).

For every potential problem set (which can be conviniently expressed as bit mask) we need to check if it satisfies all needed criteria. We can simply find the sum of problem complexities and also the difference between the most difficult and the easiest problems in linear time, iterating over the problems that we included in our current set/bitmask. If this problem set can be used, we increase the answer by one.

Complexity of this solution is O(2n·n).


AC代码(深搜):

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int a[20];
int n,l,r,x;
int ans;

 void dfs(int minn,int maxn,int sum,int pos){
    if(sum > r)
        return;
    if(sum >= l && sum <= r && maxn-minn >= x)
        ans++;
    for(int i = pos; i <= n; i++)
        dfs(minn,a[i],sum+a[i],i+1);
 }

int main(){
    while(~scanf("%d%d%d%d",&n,&l,&r,&x)){
        ans = 0;
        for(int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        for(int i = 1; i <= n-1; i++)
            for(int j = i+1; j <= n; j++)
                dfs(a[i],a[j],a[i]+a[j],j+1);
        printf("%d\n",ans);
    }
    return 0;
}

AC代码(状态压缩):

#include <iostream>
#include <cstdio>
#include <algorithm>
#define INF 0xfffffff
using namespace std;

int a[20];
int tmp[20];
int n,l,r,x;
int ans;

int main(){
    for(int i = 1; i <= 15; i++)
        tmp[i] = 1<<(i-1);
    while(~scanf("%d%d%d%d",&n,&l,&r,&x)){
        ans = 0;
        for(int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        for(int i = 1; i <=(1<<n); i++){
            int sum = 0;
            int minn = INF,maxn = -1;
            for(int j = 1; j <= n; j++){
                if(i & tmp[j]){
                    sum += a[j];
                    minn = min(minn,a[j]);
                    maxn = max(maxn,a[j]);
                }
            }
            if(sum >= l && sum <= r && maxn-minn >= x)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

4.More is better

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1856

解题思路:

用并查集查找每个集合的数量,求最大值。。。

这题有个坑,就是当n == 0的时候,应该输出1.。。。(因为这个wrong了n次。。。)

AC代码:

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

int pa[10000005];
map<int,int> m;

int findset(int x){
    if(x != pa[x])
        pa[x] = findset(pa[x]);
    return pa[x];
}

int main(){
    int n;
    while(~scanf("%d",&n)){
        if(n == 0){
            printf("1\n");
            continue;
        }
        for(int i = 0; i <= 10000000; i++)
            pa[i] = i;
        m.clear();
        int x,y,maxn = 0;
        for(int i = 0; i < n; i++){
            scanf("%d%d",&x,&y);
            maxn = max(maxn,x);
            maxn = max(maxn,y);
            x = findset(x);
            y = findset(y);
            if(x != y)
                pa[x] = y;
        }
        //cout<<maxn<<endl;
        for(int i = 1; i <= maxn; i++){
            pa[i] = findset(i);
            //cout<<pa[i]<<endl;
            m[pa[i]]++;
        }
        int sum = 0;
        for(map<int,int>::iterator it = m.begin(); it != m.end(); it++)
            sum = max(sum,it->second);
        printf("%d\n",sum);
    }
    return 0;
}


5.Prime Gap

题目链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4519

解题思路:

求1到1299709中任意一个数,如果该数为素数,则输出0,反之,则输出相邻两项之间素数差。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int a[15000000];
int prime[100005];

int main(){
    memset(a,0,sizeof(a));
    for(int i = 2; i <= 1299709; i++){
        int t = 1299709/i;
        for(int j = 2; j <= t; j++)
            a[i*j] = 1;
    }
    int sum = 0;
    for(int i = 2; i <= 1299709; i++)
        if(!a[i])
            prime[sum++] = i;
    int n;
    while(scanf("%d",&n),n){
        for(int i = 0; i < 100000; i++){
            if(prime[i] == n){
                printf("0\n");
                break;
            }
            if(prime[i] > n){
                printf("%d\n",prime[i]-prime[i-1]);
                break;
            }
        }
    }
    return 0;
}


6.Gerald is into Art

题目链接:

http://codeforces.com/problemset/problem/560/B

解题思路:

One can snuggle pictures to each other and to edge of stand.

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
    int a1,b1,a2,b2,a3,b3;
    while(~scanf("%d%d%d%d%d%d",&a1,&b1,&a2,&b2,&a3,&b3)){
        int flag = 0;
        if(a1 < b1)
            swap(a1,b1);
        if(a2 < b2)
            swap(a2,b2);
        if(a1 < a2 || b1 < b2 || a1*b1 < a2*b2+a3*b3){
            printf("NO\n");
            continue;
        }
        int ta = a1-a2;
        int tb = b1-b2;
        if(a3 < b3)
            swap(a3,b3);
        if((a3 <= ta && b3 <= b1) || (b3 <=ta && a3 <= b1) || (b3 <= tb && a3 <= a1) || (a3 <= tb && b3 <= a1)){
            printf("YES\n");
            continue;
        }
        if(b1 >= a2){
            ta = a1-b2;
            tb = b1-a2;
            if((a3 <= ta && b3 <= b1) || (b3 <=ta && a3 <= b1) || (b3 <= tb && a3 <= a1) || (a3 <= tb && b3 <= a1)){
                printf("YES\n");
                continue;
            }
        }
        printf("NO\n");
    }
    return 0;
}


7.World Islands

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3405

解题思路:

给你n个岛,让你用n-2个桥联通n-1个岛,求使得这n-1个岛联通的最小桥的长度。注意,不能求整个最小生成树,然后剪其中一条边最大值,因为你剪去的这条边可能使剩下的岛成为两部分,只能利用最小生成树的思想,一一枚举,先隔开一个点,再求最小生成树。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define INF 0xfffffff
using namespace std;

int n;
double a[55][55];
double x[55],y[55];
int vis[55];
double dis[55];

double dist(double x1,double y1,double x2,double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double prime(int cur)
{
    int i, j, tmp;
    double sum = 0;
    double maxn = 0;
    memset(vis, 0, sizeof(vis));
    vis[1] = 1;
    vis[cur] = 1;
    for(i = 1; i <= n; i++)
        dis[i] = a[1][i];
    if(cur == 1){
        for(int i = 1; i <= n; i++)
            dis[i] = a[2][i];
        vis[1] = 0;
    }
    for(i = 1; i <= n; i++)
    {
        double Min = INF;
        for(j = 1; j <= n; j++)
        {
            if(!vis[j] && dis[j] < Min)
                Min = dis[tmp = j];
        }
        if(Min == INF)
            break;
        vis[tmp] = 1;
        sum += Min;
        for(j = 1; j <= n; j++)
        {
            if(!vis[j] && dis[j] > a[tmp][j])
                dis[j] = a[tmp][j];
        }
    }
    return sum;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i = 1; i<= n; i++)
            scanf("%lf%lf",&x[i],&y[i]);
        memset(a,0,sizeof(a));
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= i; j++)
                a[i][j] = a[j][i] = dist(x[i],y[i],x[j],y[j]);
        double ans = INF;
        for(int i = 1; i <= n; i++)
            ans = min(ans,prime(i));
        printf("%.2lf\n",ans);
    }
    return 0;
}


8.Send a Table

题目链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1761

解题思路:

http://blog.csdn.net/piaocoder/article/details/47788187

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 50000;
int phi[N+10];

int main(){
    memset(phi,0,sizeof(phi));
    phi[1] = 1;
    for(int i = 2; i <= N; i++){
        if(!phi[i]){
            for(int j = i; j <= N; j+=i){
                if(!phi[j])
                    phi[j] = j;
                phi[j] = phi[j]/i*(i-1);
            }
        }
    }
    int n;
    while(scanf("%d",&n),n){
        int ans = 0;
        for(int i = 1; i <= n; i++)
            ans += phi[i];
        ans = 2*ans-1;
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(小比赛(CodeForces 560A,UVA 11040,CodeForces 550B,HDU 1856,UVA 1644,CodeForces 560B,HDU 3405,UVA 10820))