C. Annoying Present(思维+数学)

104 篇文章 0 订阅

https://codeforces.com/problemset/problem/1009/C


思路:第一眼以为是二分啥的。但是发现并不对。

应该是每个点都有贪心的选法。

可以发现,x是每个固定的增量无法改变。变的只能是d,放中位数的时候总的倍数最小,放两端的时候总的倍数最大。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
struct P{
    LL x,d;
}a[maxn];
int main(void)
{
  ///cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;n=read();m=read();
  for(LL i=1;i<=m;i++){
     a[i].x=read();a[i].d=read();
  }
  double sum=0;
  for(LL i=1;i<=m;i++){
     if(a[i].d>=0){
        sum+=1.0*n*a[i].x+1.0*n*(n-1)/2*a[i].d;
     }
     else{
        LL t=(n+2-1)/2;
        sum+=n*a[i].x;
        LL num=0;
        if(n&1) num=1.0*(t-1)*t;
        else if(n%2==0) num=1.0*(t-1)*t+t;
        sum+=1.0*a[i].d*num;
     }
  }
  printf("%.8f\n",1.0*sum/(1.0*n));
return 0;
}

 

可以使用深度优先搜索(DFS)来解决这个问题。首先,我们可以将问题抽象为一个图,其中每个房间都是一个节点,相邻房间之间有边相连。 首先,我们需要定义一个辅助函数,用于检查给定的坐标是否在合法的范围内: ```cpp bool isValid(int x, int y, int n, int m) { return (x >= 1 && x <= n && y >= 1 && y <= m); } ``` 接下来,我们可以使用DFS来遍历所有可能的移动方式。我们从Vika的起始位置开始,依次向四个方向移动,然后递归地对每个朋友进行相同的操作。如果在递归过程中,任何一个朋友与Vika在同一个房间,则说明Vika会被抓住。 以下是完整的C++代码实现: ```cpp #include <iostream> #include <vector> using namespace std; bool isValid(int x, int y, int n, int m) { return (x >= 1 && x <= n && y >= 1 && y <= m); } bool dfs(int vx, int vy, vector<pair<int, int>>& friends, vector<vector<bool>>& visited, int n, int m) { if (vx == 0 && vy == 0) { // Vika has successfully escaped return true; } visited[vx][vy] = true; // Possible neighbors' coordinates vector<pair<int, int>> neighbors = {{vx-1, vy}, {vx+1, vy}, {vx, vy-1}, {vx, vy+1}}; for (auto neighbor : neighbors) { int nx = neighbor.first; int ny = neighbor.second; if (isValid(nx, ny, n, m) && !visited[nx][ny]) { bool caught = false; for (auto friendCoord : friends) { int fx = friendCoord.first; int fy = friendCoord.second; if (nx == fx && ny == fy) { caught = true; break; } } if (!caught && dfs(nx, ny, friends, visited, n, m)) { return true; } } } return false; } string canVikaEscape(int n, int m, int vx, int vy, vector<pair<int, int>>& friends) { vector<vector<bool>> visited(n+1, vector<bool>(m+1, false)); if (dfs(vx, vy, friends, visited, n, m)) { return "Yes"; } else { return "No"; } } int main() { int n, m, vx, vy, k; cin >> n >> m >> vx >> vy >> k; vector<pair<int, int>> friends(k); for (int i = 0; i < k; i++) { cin >> friends[i].first >> friends[i].second; } cout << canVikaEscape(n, m, vx, vy, friends) << endl; return 0; } ``` 该代码首先读取输入的n,m,vx,vy和k值。接下来,它读取k个朋友的坐标,并调用canVikaEscape函数来判断Vika是否能逃脱。最后,它输出结果。 希望这可以帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值