2017ccpc哈尔滨站题解合集

参考博客:1.http://blog.csdn.net/my_sunshine26/article/details/78516355
2.


A Simple Stone Game
Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 262144/262144 K(Java/Others)
Total Submission(s): 524    Accepted Submission(s): 107

Problem Description
After he has learned how toplay Nim game, Bob begins to try another stone game which seems much easier.

The game goes like this: one player starts the game with N piles of stones. There is ai stoneson the i thpile. On one turn, the player can move exactly one stone from one pile toanother pile. After one turn, if there exits a number x(x>1) such that for each pile bi isthe multiple of x where bi is the number of stone of thethis pile now), the game will stop. Now you need to help Bob to calculate theminimum turns he need to stop this boring game. You can regard that 0 is the multiple of anypositive number.


Input
The first line is the numberof test cases. For each test case, the first line contains one positive number N(1≤N≤100000), indicating the number ofpiles of stones.

The second line contains N positive number, the i th number ai(1≤ai≤100000) indicating the number ofstones of the i th pile.


The sum of N ofall test cases is not exceed 5105.


Output
For each test case, output ainteger donating the answer as described above. If there exist a satisfiednumber x initially,you just need to output 0. It's guaranteed that there exists at least one solution.


Sample Input
2
5
1 2 3 4 5
2
5 7


Sample Output
2
1

【题意】

现在有n堆石子,且已知每堆石子的数量,每次操作你可以从任一堆中取一颗石子放到另一堆中,问最少操作几次使得每一堆石子的数量均是某一个数的倍数。

【思路】

显然,最后对于任意的a[i],满足a[i]%x==0,易得x是每一堆石子数的因子,那么x也是所有石子数量总和的因子。

那么我们只要分解总和的质因子,然后暴力枚举质因子求出最小的操作数即可。

具体细节见代码。

【PS】 注意用long long型


#include <cstdio>  
#include <vector>  
#include <cstring>  
#include <algorithm>  
using namespace std;  
#define mst(a,b) memset((a),(b),sizeof(a))  
#define rush() int T;scanf("%d",&T);while(T--)  

typedef long long ll;  
const int maxn = 100005;  
const ll mod = 10;  
const int INF = 0x3f3f3f3f;  
const double eps = 1e-9;  

int n;  
ll a[maxn];  
vector<ll>vec;  
vector<ll>mp;  

int main()  
{  
    rush()  
    {  
        scanf("%d",&n);  
        vec.clear();  
        ll sum=0;  
        for(int i=0;i<n;i++)  
        {  
            scanf("%I64d",&a[i]);  
            sum+=a[i];  
        }  
        for(ll i=2;i*i<=sum;i++)           //求出总和的质因子  
        {  
            if(sum%i==0)  
            {  
                vec.push_back(i);  
                while(sum%i==0)  
                {  
                    sum/=i;  
                }  
            }  
        }  
        if(sum>1) vec.push_back(sum);  
        sort(vec.begin(),vec.end());  
        ll ans=1e18;  
        for(int i=0;i<vec.size();i++)           //枚举质因子  
        {  
            sum=0;  
            mp.clear();  
            for(int j=0;j<n;j++)  
            {  
                int tmp=a[j]%vec[i];  
                if(tmp)  
                {  
                    mp.push_back(tmp);  
                    sum+=tmp;                 //距离倍数的总差距  
                }  
            }  
            sort(mp.begin(),mp.end());  
            ll cnt=0;  
            //从接近倍数的石子堆开始考虑,不从小的开始考虑的原因是不确定这堆是否被拿去石子  
            for(int j=mp.size()-1;j>=0;j--)     
            {  
                cnt+=vec[i]-mp[j];            //变成倍数需要几颗石子,即操作次数  
                sum-=vec[i];                  //每次能使总差距减少vec[i]  
                if(sum<=0) break;  
            }  
            ans=min(ans,cnt);  
        }  
        printf("%I64d\n",ans);  
    }  
    return 0;  
}  
 2017CCPC哈尔滨 F:Permutation(构造)
题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1006&cid=784
题意:
让你构造一个1到n的全排列,满足对于所有的i>=3,都有a[i]%|a[i]-a[i-2]|==0

思路:假设n=10
构造出的序列就是:1 6 2 7 3 8 4 9 5 10
OK

//2017CCPC哈尔滨--F  
#include<stdio.h>  
int a[100005];  
int main(void)  
{  
    int T, n, i, now;  
    scanf("%d", &T);  
    while(T--)  
    {  
        scanf("%d", &n);  
        now = 0;  
        for(i=1;i<=n;i+=2)  
            a[i] = ++now;  
        for(i=2;i<=n;i+=2)  
            a[i] = ++now;  
        printf("%d", a[1]);  
        for(i=2;i<=n;i++)  
            printf(" %d", a[i]);  
        printf("\n");  
    }  
    return 0;  
}  
2017CCPC哈尔滨 D:X-Men
原创 2017111118:10:28 59
题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=784

题意:
给一棵树,某些点上有人,每个时刻每个人都会往一个距离大于1的人的方向前进1个,
问期望多久所有人都在距离1以内

思路:
树上最远两个人的距离/2就是答案(向下取整)
因为n只有1000,所以可以n²暴力
[cpp] view plain copy
//2017CCPC哈尔滨--D  
#include<stdio.h>  
#include<string.h>  
#include<algorithm>  
#include<vector>  
using namespace std;  
vector<int> G[1005];  
int ans, k[1005];  
void Sech(int u, int p, int len)  
{  
    int i, v;  
    if(k[u])  
        ans = max(ans, len);  
    for(i=0;i<G[u].size();i++)  
    {  
        v = G[u][i];  
        if(v==p)  
            continue;  
        Sech(v, u, len+1);  
    }  
}  
int main(void)  
{  
    int T, n, m, x, y, i;  
    scanf("%d", &T);  
    while(T--)  
    {  
        scanf("%d%d", &n, &m);  
        for(i=1;i<=n;i++)  
            G[i].clear();  
        memset(k, 0, sizeof(k));  
        for(i=1;i<=m;i++)  
        {  
            scanf("%d", &x);  
            k[x] = 1;  
        }  
        for(i=1;i<=n-1;i++)  
        {  
            scanf("%d%d", &x, &y);  
            G[x].push_back(y);  
            G[y].push_back(x);  
        }  
        ans = 0;;  
        for(i=1;i<=n;i++)  
        {  
            if(k[i])  
                Sech(i, 0, 0);  
        }  
        printf("%d.00\n", ans/2);  
    }  
    return 0;  
}  
题意:给你n个点,让你找一个圆心和一个半径,使得已知的n个点中至少有(n+1)/2个点在圆上,
输出任意一种合法情况。
题解:对于圆,我们知道三点可以确定一个圆:http://blog.csdn.net/liyuanbhu/article/details/52891868
但是我们不能n^3来找,不然会炸,我们可以采取随机的方法(一丝扣死咪?),第一次用随机操作,比赛时
完全没想到这样搞(可能因此错失了银牌?),随机出三个点后,按照正常的判断即可。我们可以随机多次使得
正确性尽可能的大。。。
[cpp] view plain copy
#include<math.h>  
#include<stdio.h>  
#include<string.h>  
#include<stdlib.h>  
#include<algorithm>  
using namespace std;  
#define maxn 100005  
#define eps 1e-6  
struct node1  
{  
    double x,y,r;  
}a[maxn];  
int n;  
double dis(node1 a,node1 b)  
{  
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));  
}  
bool eq(double a,double b)  
{  
    if(fabs(a-b)<=eps)  
        return 1;  
    return 0;  
}  
bool gongxian(node1 a,node1 b,node1 c)//判断三点是否共线  
{  
    if(eq((b.x-a.x)*(c.y-a.y),(c.x-a.x)*(b.y-a.y)))  
        return 1;  
    return 0;  
}  
node1 yuanxin(node1 a,node1 b,node1 c)//求圆心坐标  
{  
    double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/2;  
    double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/2;  
    double d=a1*b2-a2*b1;  
    node1 res;  
    res.x=a.x+(c1*b2-c2*b1)/d;  
    res.y=a.y+(a1*c2-a2*c1)/d;  
    return res;  
}  
bool judge(node1 c)  
{  
    int num=0;  
    for(int i=0;i<n;i++)  
        if(eq(c.r,dis(c,a[i])))  
            num++;  
    if(num>=(n+1)/2)  
        return 1;  
    return 0;  
}  
int main(void)  
{  
    int T;  
    scanf("%d",&T);  
    while(T--)  
    {  
        scanf("%d",&n);  
        for(int i=0;i<n;i++)  
            scanf("%lf%lf",&a[i].x,&a[i].y);  
        if(n==1)  
            printf("%lf %lf %lf\n",a[0].x+1,a[0].y,1.0);  
        else if(n<=4)  
        {  
            node1 ans;  
            ans.x=a[0].x+a[1].x;ans.y=a[0].y+a[1].y;  
            printf("%lf %lf %lf\n",ans.x/2.0,ans.y/2.0,dis(a[0],a[1])/2.0);  
        }  
        else  
        {  
            while(1)  
            {  
                int t1,t2,t3;  
                t1=rand()%n;  
                t2=rand()%n;  
                t3=rand()%n;  
                if(gongxian(a[t1],a[t2],a[t3]))  
                    continue;  
                node1 c=yuanxin(a[t1],a[t2],a[t3]);  
                c.r=dis(c,a[t1]);  
                if(judge(c))  
                {  
                    printf("%lf %lf %lf\n",c.x,c.y,c.r);  
                    break;  
                }  
            }  
        }  
    }  
    return 0;  
}  


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值