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

本文精选了八道不同类型的编程挑战题目,并提供了详细的解题思路及AC代码实现。涉及算法包括完全搜索、状态压缩、并查集等,适合算法初学者及竞赛选手练习。

比赛链接:

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.youkuaiyun.com/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;
}


【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值