German Collegiate Programming Contest 2018​ 7.19(计蒜客)

本文解析了图论中的最长路径问题及数塔问题,并提供了一种求解特定数学方程的有效算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://www.jisuanke.com/contest/1407

C. Coolest Ski Route

题意:求一个有向图中的最长路

题解:一开始是没这么多想,直接用dfs和bfs试了下,从图中入度为0的结点开始搜索,然后超时,后来明白要用到记忆化搜索,记录每个点的搜索距离,当下次搜到某个已经搜过的点时不必继续下去,用保存的结果

超时代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#define maxn 1005
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
struct node
{
   int to,w;
};
struct knode
{
   int d,sz;
};
vector<node>v[maxn];
bool book[maxn],vis[maxn];
int n,m,a,b,c,sum,maxi,ans;
void dfs(int e)
{
   if(!v[e].size())
   {
      if(sum>maxi)
         maxi=sum;
      return ;
   }
   for(int i=0;i<v[e].size();i++)
   {
      if(vis[v[e][i].to]==false)
      {
         vis[v[e][i].to]=true;
         sum+=v[e][i].w;
         dfs(v[e][i].to);
         vis[v[e][i].to]=false;
         sum-=v[e][i].w;
      }
   }
}
void bfs(int e)
{
   queue<knode>q;
   knode h,t;
   h.d=e;h.sz=0;
   q.push(h);
   memset(vis,false,sizeof(vis));
   vis[e]=true;
   while(!q.empty())
   {
      knode h=q.front();
      q.pop();
      for(int i=0;i<v[h.d].size();i++)
      {
         if(vis[v[h.d][i].to]==false)
         {
            //vis[v[h.d][i].to]=true;
            t.d=v[h.d][i].to;
            t.sz=v[h.d][i].w+h.sz;
            //cout << t.d << ' ';
            //cout << t.sz <<endl;
            q.push(t);
            if(!v[t.d].size())
            {
               if(t.sz>maxi)
                  maxi=t.sz;
            }
         }
      }
   }
}
int main()
{
   ios::sync_with_stdio(false);
   cin>>n>>m;
   for(int i=0;i<m;i++)
   {
      cin>>a>>b>>c;
      node t;
      t.to=b;t.w=c;
      v[a].push_back(t);
      book[b]=true;
   }
   /*for(int i=1;i<=n;i++)
   {
      if(book[i]==false)
      {
         memset(vis,false,sizeof(vis));
         maxi=-1;sum=0;
         dfs(i);
      }
      ans=max(ans,maxi);
   }
   cout << ans << endl;*/
   for(int i=1;i<=n;i++)
   {
      if(book[i]==false)
      {
         //cout << i <<endl;
         memset(vis,false,sizeof(vis));
         maxi=-1;
         bfs(i);
      }
      ans=max(ans,maxi);
   }
   cout << ans << endl;
   return 0;
}

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define maxn 1005
#define inf 0x3f3f3f3f
#include <cstring>
#include <vector>
#define LL long long
using namespace std;
int dp[maxn];
struct node
{
   int to,w;
};
vector<node>v[maxn];
bool book[maxn],vis[maxn];
int n,m,a,b,c,sum,maxi,ans;
int rec(int e)
{
   if(vis[e])
      return dp[e];
   for(int i=0;i<v[e].size();i++)
   {
      dp[e]=max(dp[e],rec(v[e][i].to)+v[e][i].w);
   }
   vis[e]=true;
   return dp[e];
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
       cin>>a>>b>>c;
       node t;
       t.to=b;t.w=c;
       v[a].push_back(t);
       book[b]=true;
    }
   for(int i=1;i<=n;i++)
   {
      if(book[i]==false)
      {
         ans=max(ans,rec(i));
      }
   }
   cout << ans << endl;
   return 0;
}

D. Down the Pyramid

题意:数塔,给出一行n个数字,确定第二行n+1个数字满足t[i]=a[i-1]-t[i-1]的方案数

题解:一开始是想到是先找到最小的值下标minp,让t[minp]等于0开始枚举到a[minp],t[minp]每增加1,其t[minp+2*k-1]就减小1,直到t[minp+2*k-1]的最小值减到0,累加次数,这种解法wrong。 后来看了官方题解,直接让t[0]赋值0,记录 

 low=max(low,-t[2*k]),high=min(high,t[2*k+1]),ans=max(0ll,high-low+1)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include <set>
#include <functional>
#include <map>
#include <vector>

using namespace std;
#define maxn 1000005
#define INF 0x3f3f3f3f
typedef long long LL;
LL n,a[maxn],t[maxn];
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
    }
    t[0]=0;
    for(int i=1;i<=n;i++)
    {
       t[i]=a[i-1]-t[i-1];
    }
    LL l,h;
    l=0;h=INF;
    for(int i=0;i<=n;i+=2)
    {
       l=max(l,-t[i]);
    }
    for(int i=1;i<=n;i+=2)
    {
       h=min(h,t[i]);
    }
    cout << max(0ll,h-l+1) << endl;
    return 0;
}

H. Hyper Illuminati

题意:给定一个数m,求最小的n,s满足 1^(n-1)+2^(n-1)+3^(n-1)+...+s^(n-1)=m

题解:因为m最大为10^16,2^60>10^16,n可从3到60循环枚举,对每一个n,看是否有满足条件的s

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define maxn 1005
#define inf 0x3f3f3f3f
#include <cstring>
#include <vector>
#define LL long long
using namespace std;
LL m;
int main()
{
   cin>>m;
   for(int i=3;i<=60;i++)
   {
      int bz=0;
      LL t=1;
      LL sum=0;
      while(sum<m)
      {
         bz++;
         t=1;
         for(int j=1;j<i;j++)
         {
            t*=bz;
         }
         sum+=t;
      }
      //cout << sum << endl;
      if(sum==m)
      {
         cout << i << ' ' << bz << endl;
         return 0;
      }
   }
   cout << "impossible\n";
   return 0;
}

 

截至目前,尚未有2024年四川省级大学生程序设竞赛的具体题目或题解公开发布。然而,可以基于以往的比赛内容推测可能涉及的类型和主题。 通常情况下,四川省大学生程序设竞赛(Sichuan Provincial Collegiate Programming Contest)中的A题往往是一个相对基础但具有挑战性的算法问题,旨在测试参赛者的逻辑思维能力和编程技巧。例如,在2021年的比赛中,A题“Chuanpai”的核心在于通过模拟来处理周期性变化的行为模式[^3]。 对于类似的题目,解决的关键通常是识别并利用某种规律或者周期特性来进行高效算。以下是针对该类问题的一个通用解决方案框架: ### 解决方案框架 假设未来某道A题涉及到周期行为的变化,则可以通过如下方式实现其基本逻辑: #### 周期检测与状态更新 ```python def simulate_rounds(people, preferences): rounds = 0 while not is_stable_state(people): # 判断当前状态是否稳定 update_people_based_on_preferences(people, preferences) # 更新每个人的状态 rounds += 1 return rounds, people def is_stable_state(people): # 定义稳定性条件 pass def update_people_based_on_preferences(people, preferences): n = len(preferences) new_people = [] for i in range(n): if (preferences[i] % 2 == 0 and i % 2 == 0) or \ (preferences[i] % 2 != 0 and i % 2 != 0): new_people.append(add_dish(people[i])) else: new_people.append(eat_dish(people[i])) global people people = new_people[:] def add_dish(person): person['dishes'] += 1 return person def eat_dish(person): if person['dishes'] > 0: person['dishes'] -= 1 return person ``` 上述代码片段展示了如何根据偏好列表`preferences`动态调整人员数组`people`的状态,并持续迭代直到达到某个稳定的终止条件为止。 尽管目前无法确切得知2024年度的确切考题细节,但从过往经验来看,比赛倾向于考察选手们对数据结构、算法优化以及边界情况处理的理解程度。 ### 结论 综上所述,虽然具体到2024年的赛事详情尚不可知,但是通过对历年真题的学习研究可以帮助预测可能出现的方向及其应对策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值