codeforces #380 div2

1.字符串替换ogo+go…换成***

思路:找ogo记录g位置,做初步替换和标记,非目标字母直接输出,

间隔为2的判断是否一个为标记g,一个为非标记做***替换

#include<iostream>
using namespace std;

bool mark[110] = { 0 };
int tol = 0;
int main()
{
	char a[110];
	int b[110];
	int n, tol = 0, cnt = 0;
	cin >> n >> a;
	for (int i = 1; i < n-1; i++)
	{
		if (a[i] == ‘g‘&&a[i - 1] == ‘o‘&&a[i + 1] == ‘o‘)b[tol++] = i;
	}
	for (int i = 0; i < tol; i++)
	{
		mark[b[i]] = 1;
		a[b[i] - 1] = ‘1‘;
		a[b[i] + 1] = ‘1‘;
	}
	for (int i = 0; i < n; i++)
	{
		if (!mark[i] && a[i] >= ‘a‘&&a[i] <= ‘z‘)cout << a[i];
		if (mark[i] && !mark[i + 2])cout << "***";
	}

	return 0;
}

2.

题意:给出一个矩阵,对于每个0统计其上下左右有几个方向是1,该点的对应值就是几。计算矩阵中所有0的对应值之和。

考虑O(n*m)左右的时间复杂度

方法一:

先跑一次纵向,求出每个点在纵向上的对应值之和。再跑一次横向,求出每个点在横向上的对应值之和。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace  std;
const int maxn = 1000 + 10;
typedef long long LL;
const int INF = 1e9 + 10;
int a[maxn][maxn];
int main()
{
	int n, m;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			scanf("%d", &a[i][j]);
		int ans = 0;
		for (int i = 0; i < n; ++i)
		{
			int cnt = 0, flag = 0;
			for (int j = 0; j < m; ++j)
			{
				if (a[i][j] == 0 && (!flag))
					cnt++;
				else if (a[i][j] == 1)
				{
					ans += cnt;
					cnt = 0;
					flag = 1;
				}
				else if (a[i][j] == 0 && flag)
				{
					ans++;
					cnt++;
				}
			}
		}
		for (int j = 0; j < m; ++j)
		{
			int cnt = 0, flag = 0;
			for (int i = 0; i < n; ++i)
			{
				if (a[i][j] == 0 && (!flag))
					cnt++;
				else if (a[i][j] == 1)
				{
					ans += cnt;
					cnt = 0;
					flag = 1;
				}
				else if (a[i][j] == 0 && flag)
				{
					ans++;
					cnt++;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

方法二:

行累加,列累加,拿行举例,行判断左右,从0->当前列求和,如果0<当前列 就可以往左照,如果最后一列>当前列,就可以往右照。

#include <iostream>
#include <stdio.h>
using namespace std;
int mp[1005][1005];
int row[1005][1005];
int col[1005][1005];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&mp[i][j]);
            row[i][j]=row[i][j-1]+mp[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            col[i][j]=col[i-1][j]+mp[i][j];
        }
    }  

    int sum=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j]==0)
            {
                if(row[i][j]!=0)
                {
                    sum++;
                }
                if(row[i][m]!=row[i][j])
                {
                    sum++;
                }
                if(col[i][j]!=0)
                {
                    sum++;
                }
                if(col[i][j]!=col[n][j])
                {
                    sum++;
                }
            }
        }
    }
    cout<<sum<<endl;  

    return 0;
}  

3.二分在t时间到达终点所需的最小油量

题意:某人在起点处,到终点的距离为s。 汽车租赁公司提供n中车型,每种车型有属性ci(租车费用),vi(油箱容量)。 车子有两种前进方式 :①. 慢速:1km消耗1L汽油,花费2分钟。 ②.快速:1km消耗2L汽油,花费1分钟。 路上有k个加油站,加油不需要花费时间,且直接给油箱加满。 问在t分钟内到达终点的最小花费是多少?(租车子的费用)  若无法到达终点,输出-1

题解:

不能到达终点的情况:

①.油箱容量最大的汽车在一个加油站到下一个加油站的途中油量耗尽了。

②.全程用快速跑,也不能在t时间内到达终点。

我们可以先求出在t时间内到达终点的最小油箱容量,再在大于等于这个油箱容量中找租车费用最小的车子。 找最小油箱容量用二分查找就行了。 然后再判定不能到到达终点的情况就行了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2 * 1e5 + 10;
#define  LL long long
int c[maxn], v[maxn], g[maxn];
int n, s, k, t, flag;

bool judge(LL mid)
{
	LL time = 0;
	for (int i = 1; i < k; i++)
	{
		LL dix = g[i] - g[i - 1];
		if (dix>mid)             //油量mid不足以支撑以慢速到达下一个加油站
			return false;
		LL fv = (mid - dix) * 2;//fv+sv=mid  fv/2+sv=dix 解这个方程组
		LL sv = mid - fv;
		if (sv < 0)             //sv为负数表示,可以全程用快速从上一个加油站跑到当前加油站
			time += dix;
		else
			time += fv / 2 + sv * 2;
	}
	if (time <= t)
	{
		flag = 1;               //全程快速能在t时间内到达
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	while (scanf("%d%d%d%d", &n, &k, &s, &t) != EOF)
	{
		for (int i = 0; i < n; ++i)
			scanf("%d%d", &c[i], &v[i]);
		for (int i = 1; i <= k; ++i)
			scanf("%d", &g[i]);
		g[0] = 0;//加入起点
		g[k + 1] = s;//加入终点
		k += 2;
		sort(g, g + k);           //将油站按位置排序,给出的是乱序
		LL left = 0, right = s * 2, mid;
		flag = 0;
		while (left < right)
		{
			mid = (left + right) / 2;
			if (judge(mid))
				right = mid;
			else
				left = mid + 1;
		}
		if (!flag)               //全程快速也不能在t时间内到达
		{
			printf("-1\n");
			continue;
		}
		int ans = 1e9 + 10;
		for (int i = 0; i < n; i++)
		{
			if (v[i] >= left)
				ans = min(ans, c[i]);
		}
		if (ans == 1e9 + 10)//表示没有足够大油量的汽车
			printf("-1\n");
		else printf("%d\n", ans);
	}
	return 0;
}

4.题意:有n个位置,这里面包含a条船,每条船占b个位置,不知道船的位置。Galya 之前射击过k次,k次都没有打中船。 给出n长度的字符串,0表示未知位置,1表示被Galya射击过的没有船的位置。问要保证 Galya至少射击中一条船,需要再射击几次,并输出这些位置编号。

题解:先算出可能存在船的位置量num,并且记录下每条船的最后一个位置编号。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2 * 1e5 + 10;
char str[maxn];
int ans[maxn];
int main()
{
	int n, a, b, k;
	while (scanf("%d%d%d%d", &n, &a, &b, &k) != EOF)
	{
		scanf("%s", str + 1);
		int m = 0, cnt = 0;
		for (int i = 1; i <= n; ++i)
		{
			if (str[i] == ‘0‘)
			{
				cnt++;
				if (cnt == b)
				{
					ans[m++] = i;
					cnt = 0;
				}
			}
			else if (str[i] == ‘1‘)
				cnt = 0;
		}
		printf("%d\n", m + 1 - a);
		printf("%d", ans[0]);
		for (int i = 1; i <= m - a; ++i)
			printf(" %d", ans[i]);
		printf("\n");
	}
	return 0;
}
时间: 2024-12-14 18:05:39

codeforces #380 div2的相关文章

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

Codeforces #246(div2)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <ti

Codeforces #245(div2)

A:A. Points and Segments (easy) 题目看了n久,开始觉得尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让你找一种选择方案使得m个区间内的蓝球和红球数量之差不超过1. 开始想过用dfs,不过这只是div2的A题而已.. 然后想了下,直接输出010101序列不就可以么. 交了一发,发现要先排个序,再输出就可以了. AC代码: #include<iostream> #include<cstdio&g

codeforces#327 div2

codeforces#327 div2 这场状态不好有点可惜,题目都不难,而且很好.. A题:水题. #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef lo

codeforces#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l

Codeforces 258 Div2

A题,n*m根木棍,相交放置,轮流取走相交的两根,最后谁不能行动,则输掉. min(n,m)&1 为1则先取者赢. B题,给定一个长度为n,且各不相同的数组,问能否通过交换连续一段L....R使得变成单调递增. 如果一开始就是递增的,那么直接输出L...R就是1 1,交换一个就行了:否则判断中间是否有且一段单调递减,且两端交换后使得数组递增. 代码: 1 //Template updates date: 20140718 2 #include <iostream> 3 #include

Codeforces #263 div2 解题报告

比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注册的时候很想好好的做一下,但是网上喝了个小酒之后,也就迷迷糊糊地看了题目,做了几题,一觉醒来发现rating掉了很多,那个心痛啊! 不过,后来认真的读了题目,发现这次的div2并不是很难! 官方题解:http://codeforces.com/blog/entry/13568 A. Appleman and Easy Task 解析: 一个水题,判断每个细胞周围是否都是有偶数个相邻细胞.   代码

codeforces#333 div2 B. Approximating a Constant Range

http://codeforces.com/contest/602/problem/B 这道题的两种做法, 滑窗+线段树查询区间最值: #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int