Codeforces Round #775 ( Div.2)

这篇博客探讨了三款不同的游戏策略问题。A篇解析了一种跳跃游戏,关键在于理解一次最多能跳跃的距离;B篇涉及球传递,通过比较最大传球次数和总次数确定所需球的数量;C篇则关注矩阵中相同颜色方格的曼哈顿距离之和的计算。文章通过算法解决这些问题,提供了解题思路和优化方法。
摘要由CSDN通过智能技术生成

A. Game
只有一个地方,读题的时候一句英文理解错了
You can jump between adjacent locations for free, as well as no more than once jump from any location with land i to any location with land i+x, spending x coins (x≥0)。如果需要花费,那么只能跳跃一次。

#include <bits/stdc++.h>
//dfs 大法师
#define ll long long
using namespace std;
const int mod=20100403;
const int Inf=0x3f3f3f3f;
inline int read()
{
	int 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;
}
int m1[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int m2[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
void Get_Time()
{
    time_t timep;
 
    time(&timep); //获取从1970至今过了多少秒,存入time_t类型的timep
    printf("%s", ctime(&timep));//用ctime将秒数转化成字符串格式,输出:Thu Feb 28 14:14:17 2019
}
int x[200005];
void solve()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int a=1;a<=n;a++)
        {
            cin>>x[a];
        }
        int d=1;
        int flag=1;
        while(x[n]==1)n--;
        n++;
        if(n==1){cout<<0<<endl;continue;}
        while(x[d]==1)d++;
        d--;
              cout<<n-d<<endl;
    }
 
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //clock_t st,en;
    //st=clock();
    //Get_Time();
    solve();
    //en=clock();
    //cout<<(double)(en-st)/CLOCKS_PER_SEC<<endl;
    return 0;
}

B. Game of Ball Passing
题意:n个人互相传球,给出没人传球的次数,问最少需要几个求才能完成这项运动。
分析:纯思维题,只需要将传球最多的次数与其余传球次数的和进行比较如果sum大于等于max,那么一个球绝对就够了(如果sum等于max,很好理解,其余人只需要将球传给max那个人就可以了;如果sum大于max,那么就让球先在sum内部传,直到sum等于max!!)
如果sum小于max,那么就还得需要(max-sum)个球,分别让max传出去。

#include <bits/stdc++.h>
//dfs 大法师
#define ll long long
using namespace std;
const int mod=20100403;
const int Inf=0x3f3f3f3f;
inline int read()
{
	int 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;
}
int m1[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int m2[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
void Get_Time()
{
    time_t timep;
 
    time(&timep); //获取从1970至今过了多少秒,存入time_t类型的timep
    printf("%s", ctime(&timep));//用ctime将秒数转化成字符串格式,输出:Thu Feb 28 14:14:17 2019
}
int t, n;
ll mx, sum, a[100010];
void solve()
{
    cin >> t;
	while(t--){
		cin >> n, mx = 0, sum = 0;
		for(int i = 1; i <= n; i++)
			cin >> a[i], mx = max(mx, a[i]), sum += a[i];
		if(mx == 0) puts("0");
		else if(mx <= sum - mx) puts("1");
		else printf("%lld\n", mx - (sum - mx));
	}
 
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //clock_t st,en;
    //st=clock();
    //Get_Time();
    solve();
    //en=clock();
    //cout<<(double)(en-st)/CLOCKS_PER_SEC<<endl;
    return 0;
}

C. Weird Sum
题意:给出一个矩阵,每个方格都涂上颜色,求相同颜色方格之间各个方格曼哈顿距离的总和。
分析:最简单的方法,直接三重循环暴力,这样最容易理解,但会超时。所以我们需要对矩阵进行优化。要求相同颜色之间各个方格的曼哈顿距离之和,其实我们不用关心每个方格的坐标是什么,只需要将横坐标与纵坐标分别拿出来,然后分别求两者各个位置的距离之和,将二维的坐标转换成一维。
例如我们求纵坐标曼哈顿距离之和:
首先将所有纵坐标从小到大排序,然后从前往后模拟,第二个位置与第一个位置的距离,乘第一个位置后面还有几个数,乘这些数的前面有几个点。
这种做法采用了分段求解的思维方式,可以手动模拟一下。

#include<bits/stdc++.h>
using namespace std;
int n,m,a[1001][1001];
long long asw;
vector<int>v1[100001],v2[100001];
int main() {
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) {
			scanf("%d",&a[i][j]);
			v1[a[i][j]].push_back(i);
			v2[a[i][j]].push_back(j);
		}
	for(int i=1; i<=100000; i++) {
		if(!v1[i].size()) continue;
		sort(v1[i].begin(),v1[i].end());
		sort(v2[i].begin(),v2[i].end());
		for(int j=1; j<v1[i].size(); j++) {
			asw+=1ll*(v1[i][j]-v1[i][j-1])*(v1[i].size()-j)*j;
			asw+=1ll*(v2[i][j]-v2[i][j-1])*(v1[i].size()-j)*j;
		}
	}
	printf("%lld",asw);
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值