营地总结呀

 T1

1.  勇者们斗恶龙

题目ID:20301必做题100分

时间限制: 1000ms

空间限制: 524288kB

题目描述

恶龙又一次摧毁了城市,愤怒的国王决定讨伐恶龙。于是他在王国四处贴上告示,征召勇者们前来讨伐恶龙。

由于恶龙十分强大,勇者再厉害也只是凡人,一个人的力量是无法打败恶龙的,但团结起来的勇者们将是不可战胜的。恶龙的血量为X,X为整数。

每个勇士都有自己擅长的地方,都可以对某一阶段(某个血量区间内)的恶龙进行致命伤害。

现在国王要从n个勇士中选出可以完全覆盖对恶龙致命伤害的勇士,最少需要几个人?

输入格式

第一行输入一个n表示有n个勇士

第2行到第n+1行输入L和R,表示第i位勇士可以造成致命伤害的血量区间

第n+1行输入X表示恶龙的血量

输出格式

覆盖范围包括恶龙全部血量区间的数量,如果无法覆盖,则输出-1。

样例

Input 1

5 0 1 1 2 2 7 7 8 8 10 10

Output 1

5

样例解释

对每个TestSample的解释(为什么这个input会得到这个output)

数据范围

0 <= X <= 10^9

0 <= n <= 10000

题解

这题是一个区间覆盖问题(废话)

我们从区间的左端点开始进行遍历排序

​
#include<bits/stdc++.h>
#define int long long//10年OI一场空,不开long long见祖宗
using namespace std;
const int N = 1e4 + 10;
int n, x;
signed main()
{
	vector<pair<int, int>> q;//用结构体也可以
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int a, b;
		cin >> a >> b;
		q.push_back({ a,b });
	}
	cin >> x;
	sort(q.begin(), q.end());//排序
	int r = 0, temp = 0, res = 0;
	for (int i = 0; i < n; i++)
	{
		if (temp >= x)//结束条件
		{
			cout << res << "\n";
			return 0;
		}
		if (q[i].first > temp)//不可以覆盖的情况输出-1
		{
			cout << "-1" << "\n";
			return 0;
		}
		while (i < n && q[i].first <= temp)
		{
			r = max(r, q[i].second), i++;
		}
		temp = r;
		res++;
		i--;
	}
	if (temp >= x)//再判断一遍是否合法
	{
		cout << res << "\n";
		return 0;
	}
	else
	{
		cout << -1 << "\n";
	}
	return 0;
}

​

T2

2.  社交树

题目ID:20316必做题100分

最新提交:

Accepted

100 分历史最高:

Accepted

100 分

时间限制: 1000ms

空间限制: 524288kB

题目描述

有一句话叫作:“最多只需要7个人就可以认识任何人”,因为每个人都有自己的朋友,而朋友也有自己的朋友,所以在这世界上的任何一个陌生人,可能都是你朋友的朋友的朋友的朋友的朋友的朋友的朋友。当然这并不意味着我们和任何一个陌生人建立联系很容易,通过朋友来找朋友往往需要花费不小的代价。现在你只有一个朋友,但你的朋友却有很多他的朋友(朋友是相互的,你也是他的朋友)。你和朋友进行联络需要付出一定的代价,同样的你朋友联络他的朋友也有一定的代价。给你所有的人和每个人的朋友关系,求出你联络他们每个人所需要的代价。

输入格式

第一行输入一个n表示有n个人

第二行输入n个整数ai​ (1<=i<=n),表示第i个人被他的朋友联络到需要的精力。a1​表示你联系你的朋友需要的精力。

随后n-1行每行两个数,表示这两个人是朋友。(输入保证朋友之间的关系网络不会形成环)。

输出格式

输出一行,共n个数,表示你联络到他们每个人需要花费的总精力。按第二行输入的顺序输出每个人的总精力。

样例

Input 1

5

1 2 3 4 5

1 2

1 3

2 4

2 5

Output 1

1 3 4 7 8

样例解释

输入的是一个树并且保证输入结构正确

数据范围

所有数据均不超过5000

题解

这道题一定要先手磨一下样例,就会发现这题是有多模板。。。

可以看到,我把样例中的树用手画出来了,这样就很好分析了,样例输出为1 3 4 7 8 

1就是根结点的权重,

3是从根结点1到2所花费的时间就是把题目的权重加起来,1 + 2

4和3一样

7是从根结点到4结点的时间,为1 + 2 + 4 = 7;

8同7;

这样分析下来,我们就会发现,遍历一遍树就完成了!

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, x, y;
bool  t[5010][5010];
int res[5005];
struct node 
{
	int x, y;
} q[5005];
void bfs(int s)
{
	int head = 1, tail = 1;
	q[head].x = 1;
	res[1] = q[head].y;
	tail++;
	while (head < tail) 
	{

		for (int i = 1; i <= n; i++)
		{
			if (t[q[head].x][i] == 1)
			{
				q[tail].x = i;
				tail++;
				res[i] = res[q[head].x] + q[i].y;

			}

		}
		head++;
	}
}
signed main()
{

	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> q[i].y;
	}
	for (int i = 1; i < n; i++)
	{
		cin >> x >> y;
		t[x][y] = 1;
	}

	bfs(1);
	for (int i = 1; i <= n; i++)
	{
		cout << res[i] << " ";
	}
	return 0;
}

T3

T3还不太会,全网解答

时间限制: 1000ms

空间限制: 262144kB

题目描述

小信最近遇到了一道非常让他头疼的题目,如果有三个整数l,r,x(l≤r),cal(l,r,x)的定义如下:

  • 如果x<l:cal(l,r,x)=l−x
  • 如果l≤x≤r:cal(l,r,x)=0
  • 如果r<x:cal(l,r,x)=x−r

小信现在有N对整数,第i对表示为(Li​,Ri​),对于k=1,2,...,N,他可以任意选择一个整数x并计算cal(L1​,R1​,x),cal(L2​,R2​,x),...,cal(Lk​,Rk​,x)中的最大值,即max(cal(L1​,R1​,x),cal(L2​,R2​,x),...,cal(Lk​,Rk​,x))。对于每个k=1,2,...,N,请帮他找到每个k最小的max(cal(L1​,R1​,x),cal(L2​,R2​,x),...,cal(Lk​,Rk​,x))。

输入格式

第一行输入一个正整数N(1≤N≤2×105),表示整数对的个数。

接下来N行,每行两个正整数Li​和Ri​(1≤Li​≤Ri​≤109),表示一个整数对。

输出格式

输出N行,对每个k=1,2,...,N,每行输出一个整数,表示最小的max(cal(L1​,R1​,x),cal(L2​,R2​,x),...,cal(Lk​,Rk​,x))。

样例

Input 1
 

3 1 3 2 4 5 6

Output 1
 

0 0 1

Input 2
 

10 64 96 30 78 52 61 18 28 9 34 42 86 11 49 1 79 13 59 70 95

Output 2
 

0 0 2 18 18 18 18 18 18 21

样例解释

对于样例#1:

  • 1k=1时,可选1x=1。
  • 2k=2时,可选3x=3。
  • 3k=3时,可选4x=4。

数据范围

对于10%10%的数据,1≤N≤100,1≤Li​≤Ri​≤100。

对于20%20%的数据,1≤N≤1000,11≤Li​≤Ri​≤105。

对于100%100%的数据,1≤N≤105,1≤Li​≤Ri​≤109。

T4

题目ID:20296必做题100分

最新提交:

Accepted

100 分历史最高:

Accepted

100 分

时间限制: 2000ms

空间限制: 262144kB

题目描述

小信被给定两个整数 l 和 r,其中 l<r。从 l 开始,每次加 11 直到结果等于 r。每次加法操作会改变多少个数字的位数,并且改变的位数总是结果中的后缀部分。

例如:

如果l=909,则加一后得到 910910,会改变 22 个数字的位数;
如果 l=9,则加一后得到 1010,同样会改变 22 个数字的位数;
如果 l=489999,则加一后得到 490000490000,会改变 55 个数字的位数。

请输出每个测试用例中,从 l 到 r 的过程中总共改变了多少个数字的位数。

输入格式

第一行包含一个整数 t(1≤t≤104)。接下来有 t 个测试用例。

每个测试用例由两个整数 l 和 r 描述(1≤l<r≤109)。

输出格式

对于每个测试用例,计算从 l 开始加 11 直到达到 r 的过程中,总共改变了多少个数字的位数。

样例

Input 1

4 1 9 9 10 10 20 1 1000000000

Output 1

8 2 11 1111111110

数据范围

对于 10% 的数据,t≤10,r≤105
对于 20% 的数据,r≤105
对于 100% 的数据,无特殊限制。

题解

这题看到有点玄乎的样子,其实很简单,

开始设一个res = r - l;

然后两个循环,res分别减r/10和l/10就可以了(简单把)


#include<bits/stdc++.h>
#define int long long
using namespace std;
int l, r, t, res;
signed main()
{
	cin >> t;
	while (t--)
	{
		cin >> l >> r;
		res = r - l;
		for (;r > 0;)
		{
			res += r / 10;
			r /= 10;
		}
		for(;l > 0;)
		{
			res -= l / 10;
			l /= 10;
		}
		cout << res << endl;
	}
	return 0;
}

题目背景
Farmer John 养了 N 头牛,她们已经按 1∼N 依次编上了号。FJ 所不知道的是,他的所有牛都梦想着从农场逃走,去参加马戏团的演出。可奶牛们很快发现她们那笨拙的蹄子根本无法在钢丝或晃动的的秋千上站稳(她们还尝试过把自己装在大炮里发射出去,但可想而知,结果是悲惨的) 。最终,她们决定练习一种最简单的杂技:把所有牛都摞在一起, 比如说, 第一头牛站在第二头的身上, 同时第二头牛又站在第三头牛的身上…最底下的是第 N 头牛。

题目描述
每头牛都有自己的体重以及力量,编号为 ii 的奶牛的体重为 Wi,力量为 Si
当某头牛身上站着另一些牛时它就会在一定程度上被压扁,我们不妨把它被压扁的程度叫做它的压扁指数。对于任意的牛,她的压扁指数等于摞在她上面的所有奶牛的总重(当然不包括她自己)减去它的力量。奶牛们按照一定的顺序摞在一起后, 她们的总压扁指数就是被压得最扁的那头奶牛的压扁指数。

你的任务就是帮助奶牛们找出一个摞在一起的顺序,使得总压扁指数最小。

输入格式
第一行一个整数 N。
接下来 N 行,每行两个整数 Wi
和 Si
输出格式
一行一个整数表示最小总压扁指数。

样例 #1
样例输入 #1
310 3
2 5
3 3
样例输出 #1
2

题解
一道struct排序加一点复杂的贪心.

首先
那肯定是结构体排序啊定义一个结构体
开long long 开long long 开 long long
我这里用了define

#define int long long
所以int main() 要改为signed main()

struct Node
{
   int w;
   int s;
}a[注意看数据范围];
其次
那必须结构体排序啊
我们要求压扁总值最小
就要把重量重的放在下面就是把a.w排序
但压扁总值还要cow的力量大,就是把a.s排序
两个加起来就等于比较a.w + a.s排序
所以cmp里要写 x.w + x.s > y.w + y.s.

bool cmp(Node x,Node y)
{
 return x.w + x.s > y.w + y.s;
}
一个cmp表示sort的从大到小的顺序
比较重量和力量的加之

sort(a + 1,a + n + 1,cmp);
在把每个cow的重量加起来

for(int i = 1;i <= n;i++)
{
    sum += a[i].w;
}
现在就可以枚举每一个牛正在下面的方案了,这里有一个小小的文字游戏啊他是要算每个牛的总压扁指数,但是不是求min,其实应该求max,以为减去自生cow的重量和力量后,越大是不是被压扁的越小啊所以要求max。

用一个sumw 来记录每个cow的总压扁指数,然后用一个Maxx判断大小(Maxx记得给一个极小值)

int sumw = 0;
for(int i = 1;i <= n;i++)
{
    sumw = sum - (a[i].s + a[i].w); #计算总压扁指数
    if(maxx < sumw)
    {
        maxx = sumw;
    }
    sum -= a[i].w;#这里要减去下面牛的重量
}
最后
我们愉快的输出Maxx

cout<<maxx;

题目描述
时间限制: 1s 空间限制:32M
题目描述:
给定一个N∗M 方格的迷宫,每个方格最多经过一次,且迷宫里有 T 处障碍,障碍处不可通过。

在迷宫中有上下左右四种移动方式,每次只能移动一个方格的距离。数据保证起点上没有障碍。

给定起点坐标和终点坐标,问: 有多少种从起点坐标到终点坐标的行走方案。

输入格式:
第一行有三个整数:N,M,T。N 为行数,M 为列数,T 为障碍总数。(1≤N,M≤5)

第二行有起点坐标 SX,SY 和终点坐标 FX,FY。

接下来 T 行,每行为障碍点的坐标。

输出格式:
从起点到终点的行走方案总数。

样例输入1:
2 2 1
1 1 2 2
1 2

样例输出1:
1

这题
几乎是一道DFS模板题目
首先
我们有一个起点一个终点,有T个障碍的坐标,让我们找走到终点的路有几条
那我们只需构建一个N*M的图,在图上标注障碍,起点和终点,然后BFS找路即可,一边BFS一边记录一个总数ans,最后输出即可。

头文件纯属我的喜好

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#define int long long//图方便
using namespace std;
int m, n, t, ans, fx, fy, xs, ys;
bool vis[105][105];
int mp[105][105];
int px[] = { 0,0,1,-1 };//4个可以走的点的坐标
int py[] = { 1,-1,0,0 };
void dfs(int x, int y)
{
    if (如果到了终点)
    {
        数量++;
        return;
    }
    for (int i = 0; i <= 3; i++)
    {
        int cx = x + px[i];
        int cy = y + py[i];
        if (判断越界和是否被标记)
        {
            continue;
        }
        vis[cx][cy] = 1;//标记
        dfs(搜下去);
        vis[cx][cy] = 0;//这里求的是总数所以一定要回溯!
    }
}
signed main()//前面用了define这里要改
{
    cin >> n >> m >> t;
    cin >> xs >> ys >> fx >> fy;
    for (int i = 1; i <= t; i++)
    {
        int u, v;
        cin >> u >> v;
        建图
    }
    先给起点标记上
    dfs(开始DFS原点);
    cout << ans;//输出总数
    return 0;
}

# Summer Camp Day10 Simulation Competition Summary

Today is Summer Camp Day 10 (Day 114514) friendly third simulation match, - Me too - Happy - (sha) - Fast - (bi) - Hit - * - Score - (too low)

During the competition
I spent an hour and a half debugging the third question, but in the end, for some unknown reason, I couldn’t figure out the BFS. In the end, I even cheated and got 63 points. After thinking hard, although I got a friendly score, I didn’t get a single AC -. It’s really a pity.

Post match summary
I still can’t talk for an hour and a half on one question, otherwise it would be a bit disadvantageous, When encountering a problem, you should first think about the feasibility of the code before writing it, otherwise you will be like me, unable to tune it for 1.5 hours and fly away,话不多说,直接上题解(全网无小号,认准代码后的卬字印,超、防抄伪代码哦!);

T1 Answer to the question
Time limit: 1000ms

Space limit: 65536kB

Given a set [1,2,3,…, n], all its elements share n! Arrange the species.

List all arrangements in order of recruitment size and label them one by one. When n=3, all arrangements are as follows:

1.“123”

2.“132”

3.“213”

4.“231“

5.“312”

6.“321”

Given n and k, return the kth permutation.。

Input format
The first line inputs two integers n and k.

Output format
Output the kth sequence.

Example.Input 1
3 3

Output 1
213

Input 2
4 9

Output 2
2314

Data Range

1<=n <=9

1<=k <=n !

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#define int long long
using namespace std;
const int maxx = INT_MAX;
const int minn = INT_MIN;
const int N = 105;
int n, a[N], vis[N], m, ans;
int f(int p)
{
    if (n条件)
    {
        if (ans条件)
        {
            for (int i = 1; i <= n; i++)
            {
                输出
                ans++;
            }
        }
        ans++;
        return 0;
    }
    for (int i = 1; i <= n; i++)
    {
        if (是不是标记了)
        {
            标记
            a[p] = i;
            搜进去
            回溯
        }
    }
    return 0;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    DFS进去
    return 0;

//        __   _____
//       / / /  __  \
//      / /  | /   \ |
//     / /   | |   | |
//      | |    | |   | |
//    | | __ | |   | |
//    | |/ / | |   | |
//    | / /  | |  _| |
//    \__/   | |  \__/
//           | |
//           | |  tomato.卬  
//           \_/

T2 Answer to the question
Time limit: 1000ms

Space limit: 262144kB

Title Description Time
: 1s Space: 256M
Title description:

There are on the number line

N points, numbered as one1 to n。

Xiaoxin has M intervals, which may overlap. Xiaoxin needs to choose

one
M − 1 interval covers as many points as possible. Afterwards, Xiaoxin wants to know the number of uncovered points and the number of unselected intervals. If there are multiple intervals that meet the conditions, output the largest one.

Input format:
The first line contains two integers

n, m。

next

M rows, each row containing two integers

L ,i,R, i

Output format:
Output two integers. The first integer represents the number of the unselected interval. If there are multiple intervals that meet the criteria, output the largest one with the number from

one

Starting from 1; The second number represents the number of uncovered points.

Example 1 Input:

10 4

1 6

4 9

3 4

5 8

Four points were not covered, namely

[seven,eight,nine,ten]

[7,8,9,10];

Do not select thethree

3 intervals, there are

one

One point was not covered, namely

[ten]

[10];

Do not select the

four

4 intervals, there are

one

One point was not covered, namely

[ten]

[10] .

So I don’t choose

three

3 or

four

4 intervals, with the smallest number of points not covered, all are

One

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#define int long long
using namespace std;
const int maxx = INT_MAX;
const int minn = INT_MIN;
const int N = 1e5 + 10;
int a[N], b[N];
int l[N], r[N];
int n, m;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        输入
        差分
    }
    int cnt1 = n;
    for (int i = 1; i <= n; i++)
    {
        前缀和
        差分
    }
    int pos = 0;
    int ret = 114514;
    for (int i = 1; i <= m; i++)
    {
        if (ret大小条件)
        {
            ret = 赋值;
            pos = i;
        }
    }
    输出
    return 0;

//        __   _____
//       / / /  __  \
//      / /  | /   \ |
//     / /   | |   | |
//      | |    | |   | |
//    | | __ | |   | |
//    | |/ / | |   | |
//    | / /  | |  _| |
//    \__/   | |  \__/
//           | |
//           | |  tomato.卬  
//           \_/

T3
Time limit: 1000ms
Space limit: 262144kB
Title Description
Time: 1s Space: 256M
Title description:
There is one
A map of n × m, with an open space of ‘.’, The high-rise building is’ # ‘, and Xiaoxin’s home is’ S’.
Xiaoxin wants to find a path to exercise, which requires starting from home, not passing through the same open space, and finally returning home. For the purpose of exercising, the length of this path must be at least
four
4. That is to say, except for the endpoint and the starting point both being homes, the number of different open spaces passed through should be at least
three
3.
Xiaoxin wants to know if such a path exists. Of course, Xiaoxin cannot pass through places with high-rise buildings, so there may not be such a path.
Input format:
The first line contains two integers
n,m。
Next includes one
A map of n × m, which only contains’. ‘,’ # ‘,’ Three characters of S’.
Output format:
If there is a path to exercise the body, output ‘Yes’, otherwise output’ No '.
Example 1 Input:
4 4

#.#.
#S#.

Example 1 Output:
Yes
Example 2 Input:
4 4
.#…
#.#.
#S#.

Example 2 Output:
No

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <shared_mutex>
#include <charconv>
#include <filesystem>
using namespace std;
int n, m, t, k, maxx, a, b;
int vis[1000011];
int st;
map<pair<int, int>, char> mp;
int dxs[4] = { 自己推 };
int dys[4] = {  };
void dfs(int x, int y) {
    for (有几个方向) {
        int xx = dxs[i] + x;
        int yy = dys[i] + y; 
         if (是不是到了) {
            cout << "Yes" << endl;
            exit(0);//相当于return 0;
        }
        if (防越界) {
            标记
            mp[{xx, yy}] = '#';
            dfs(xx, yy);
            回溯
        }
    }
}
 
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char x;
            cin >> x;
            mp[{i, j}] = x;
            if (存下起点) {
                a = i; b = j;
                标记障碍
            }
        }
    }
    DFS进去
    cout << "No" << endl;
    return 0;
}
//        __   _____
//       / / /  __  \
//      / /  | /   \ |
//     / /   | |   | |
//      | |    | |   | |
//    | | __ | |   | |
//    | |/ / | |   | |
//    | / /  | |  _| |
//    \__/   | |  \__/
//           | |
//           | |  tomato.卬  
//           \_/

T4
这个我有点不会了。。。有没有人给我讲一下啊
Time limit: 1000ms
Space limit: 524288kB
Title Description
[Title Description]
Daha is already an adult, and web games were still popular in his time. He has such a game in his memory.
The character starts at position (1,1) in the maze and eventually reaches position (n, n), where there are some obstacles that cannot be walked. At the same time, some points in the maze are colored dots, and the color of the colored dots changes once per second according to the color sequence of this point. After the change, it will cycle again. For example, this colored dot will change like this, “1 2 4”, so it is 1 in the first second, 2 in the second, 4 in the third, and 1 in the fourth second, repeating in sequence. When you reach the colored point, you can choose to walk it directly up as a regular point, or you can choose to teleport it to any colored point that is currently the same color as it, and teleport it without taking time. Daha can walk up, down, left, and right, taking 1 second to complete each step. He can also choose not to move and wait for a second, because maybe after waiting for a second, the next step can be directly teleported. Daha has already learned some algorithms, and he wants to know what is the shortest time to reach (n, n)?
[Input Format]
The first line is an integer n
Next n lines, each containing n integers separated by spaces. Each integer is either 0 or 1, where 0 represents an empty space and 1 represents an obstacle. Ensure that both the starting and ending points are 0
Next line, an integer t representing the number of colored grids
Next, in line t, the first and second integers xi and yi represent the coordinates of this colored grid; The third integer mi represents the color sequence length of the i-th colored grid, followed by mi integers representing the color transformation order of the current colored point
[Output Format]
An integer representing the shortest time to reach the endpoint. If the endpoint cannot be reached, output -1
[Sample Input]
five
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 1 1 1
0 0 1 0 0
three
2 2 3 1 2 3
3 4 5 1 2 1 2 4
5 4 4 5 5 5 4
[Sample Output]
twenty-one

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <shared_mutex>
#include <charconv>
#include <filesystem>
using namespace std;
#define maxn 1145
#define maxt 11
int mp[maxn][maxn];
int mxy[maxn][maxn];
int m[maxt];
int n, t;
pair<int, int> nodem[maxt];
int col[maxt][maxt];
int dis[maxn][maxn];
struct node
{
    int x, y;
};
queue<node>q;
int dir[4][2] = { 1,0,-1,0,0,1,0,-1 };
void bfs()
{
    memset(dis, 0x3f, sizeof(dis));
    q.push({ 1,1 });
    mp[1][1] = 1;
    dis[1][1] = 0;
    while (!q.empty())
    {
        node p = .......;
        q........;
        int x = p.x, y = p.y;
        if (x ...... && y ......) {
            cout << dis[n][n] << endl;
            exit(0);
        }
        int num = .....;
        int ti = ......;
        if (num) {
            for (int k = 0; k <= 114; k++)
            {
                int c = col.....[(......) % .........;
                for (int i = 1; i <= t; i++) {
                    if (......)continue;
                    if (.........) % m[i]]) {
                        if (...........)
                        {
                            .................... = dis[x][y] + k;
                            q.push({ ....................... });
                        }
 
                    }
                }
            }
 
        }
        int xx, yy;
        for (int i = 0; i < 4; i++) {
            xx = x + dir[i][0];
            yy = y + dir[i][1];
            if (....................)continue;
            if (.........................) {
                dis[xx][yy] = dis[x][y] + 1;
                q.push({ xx,yy });
            }
        }
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> mp[i][j];
        }
    }
    cin >> t;
    int tx, ty;
    for (int i = 1; i <= t; i++) {
        cin >> tx >> ty;
        nodem[i] = { ......};
        mxy[tx][ty] = ....;
        cin >> m[i];
        for (int j = 1; j <= m[i]; j++) {
            cin >> col[i][j];
        }
        col[i][0] = col[i][m[i]];
    }
    bfs();
    cout << -1 << endl;
}
//        __   _____
//       / / /  __  \
//      / /  | /   \ |
//     / /   | |   | |
//      | |    | |   | |
//    | | __ | |   | |
//    | |/ / | |   | |
//    | / /  | |  _| |
//    \__/   | |  \__/
//           | |
//           | |  tomato.卬  
//           \_/

1. 存图方式
存图方式一般有:
邻接表,前向星,vector等
邻接表

struct Arc {
    //每条边在对应在顶表中的位置
    int arc_pos;
    //边的权重
    int arc_weight;
    //边的下一个节点指针
    Arc* arc_next;
};
//顶点表
struct Ver {
    //顶点的编号(这里就式顶点数组的下表了)
    int ver_name;
    //顶点中指向第一个节点的指针
    Arc* ver_next;
    //顶点是否访问过
    bool ver_viste = false;
};

前向星

// (u, v, w): 有一条边,从u节点指向v节点,权重为w
// 在每一次添加边时,cnt都表示当前未分配的边的编号,添加边后cnt需++
void addEdge(int u, int v, int w) {
    next[cnt] = head[u];
    to[cnt] = v;
    weight[cnt] = w;
    head[u] = cnt;
    ++cnt;
}
 
vector

vector<pair<int, int>> mp[5005];//建vector
int main()
{
    int n, m;//一个N*M的图
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y, w;
        cin >> x >> y >> w;
        mp[x].push_back({y, w});//push_back进去
    }
}
               
2. 图的类型
图分为两种:
图和树,接下来就盘点一下图的不同种类吧 

先是我喜欢的树开始
第一种 ~那必须是二叉树啊~
这个是完全二叉树(图画的丑,勿喷)

微信截图_202407271936541215×1137 97.2 KB


这个是不完全二叉树

微信截图_202407271939391327×1132 84.4 KB


不完全二叉树
这个是基环树,顾名思义就是支友一个环的树

微信截图_202407271941331309×1125 106 KB


另外还有没有右子树和没有左子树的二叉树,这里我就不展开了
第2种树

不二叉的树(废话)

微信截图_202407271944491450×1132 132 KB


这种树一般不怎么好存,所以我叫它司(hao)母(yong)树顶(非常司(hao)母(yong)的树)
接下来就是庞大的图家族了
第一种
无向无环图(我画的这个也叫菊花图)

微信截图_202407271948331197×1132 78.3 KB


有向无环图

微信截图_202407271949421219×1119 88 KB


无向有环图

微信截图_202407271950221380×1141 104 KB


有向有环图

微信截图_202407271951521204×1132 114 KB


常用的树和图基本就是这些了(如果有我没有写的记得提醒我哦)

3. 关于图的基本算法
基本的算法大家最熟悉的就是DFS和BFS了
1.DFS
1为了求得问题的解,先选择某一种可能情况向前探索;
2在探索过程中,一旦发现原来的选择是错误的,就退回一步重新选择,继续向前探索;
3如此反复进行,直至得到解或证明无解

Void DFS(deep,...){
  if(找到解 || 走不下去了){
   ......//根据题意添加
    return; 
  }
  for(扩展方式){
   if(扩展方式所能达到的状态合法){
      修改操作;//根据题意添加
      标记;
      DFS(deep+1,...);
      //根据题意是否要还原
    }  
  }
 
}
2.BFS
BFS一般是解决一些连通块,最短路,走迷宫等的题目的

void BFS(){
  初始化队列Q;
  起点S入队;
  标记S已经访问;
  while(Q非空){
    取Q的队首元素U;
    U出队列;
    if(u==目标状态){
      返回结果;
     }
    for(所有与U相邻的元素){
      if(相邻的元素合法 && 未访问){
         入队;
         标记访问;
        }
    }
  }
}

DFS和BFS金典例题
1.

题目描述
机器猫被困在一个矩形迷宫里。
迷宫可以视为一个 
n×m 矩阵,每个位置要么是空地,要么是墙。机器猫只能从一个空地走到其上、下、左、右的空地。
机器猫初始时位于 
(1,1) 的位置,问能否走到 
(n,m) 位置。
输入格式
第一行,两个正整数 
n,m。
接下来 
n 行,输入这个迷宫。每行输入一个长为 
m 的字符串,# 表示墙,. 表示空地。
输出格式
仅一行,一个字符串。如果机器猫能走到 
(n,m),则输出 Yes;否则输出 No。
输入输出样例
输入 #1复制
3 5
.##.#
.#...
...#.
输出 #1复制
Yes
说明/提示
样例解释
路线如下:
(1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(2,3)→(2,4)→(2,5)→(3,5)
数据规模与约定
对于 100% 的数据,保证 1≤n,m≤100,且 (1,1) 和(n,m) 均为空地。

dfs 的方法就是找一条路走,直到碰壁后换一条走。

还有一种方法,我们可以逐层展开搜索:

搜索一步能到达的点;
搜索两步能到达的点;
搜索三步能到达的点;
这种方法我们成为广度优先搜索(宽度优先搜索,bfs)。

使用广度优先搜索就要使用到队列,存储待访问的点。
AC代码(我自己的)

#include<bits/stdc++.h>
using namespace std;
int m,n;
bool vis[105][105];
char mp[105][105]; 
int px[] = {0,0,1,-1};
int py[] = {1,-1,0,0};
void fun(int x,int y)
{
    if(x < 1 || x > n||y > m||y < 1 ||vis[x][y] == 1 || mp[x][y] == '#')
    {
        return;
    }
    vis[x][y] = 1;
    for(int i = 0;i <= 3;i++)
    {
        int cx = x + px[i];
        int cy = y + py[i];
        fun(cx,cy);
    }
}
int main()
{
    cin>>n>>m;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)
        {
            cin>>mp[i][j];
        }
    }
    fun(1,1);
    if(vis[n][m] == 1)
    {
        cout<<"Yes";
    }
    else
    {
        cout<<"No";
    }
    return 0;
}

2

# 填涂颜色
 
## 题目描述
 
由数字 $0$  组成的方阵中,有一任意形状的由数字 $1$ 构成的闭合圈。现要求把闭合圈内的所有空间都填写成 $2$。例如:$6\times 6$ 的方阵($n=6$),涂色前和涂色后的方阵如下:
 
如果从某个 $0$ 出发,只向上下左右 $4$ 个方向移动且仅经过其他 $0$ 的情况下,无法到达方阵的边界,就认为这个 $0$ **在闭合圈内**。闭合圈不一定是环形的,可以是任意形状,但保证**闭合圈内**的 $0$ 是连通的(两两之间可以相互到达)。
 
```plain
0 0 0 0 0 0
0 0 0 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 1 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 1 2 1
1 1 1 1 1 1
输入格式
每组测试数据第一行一个整数 $n(1 \le n \le 30)$。

接下来 n 行,由 0 和 1 组成的 n×n 的方阵。

方阵内只有一个闭合圈,圈内至少有一个 $0$。

输出格式
已经填好数字 2 的完整方阵。

样例 #1
样例输入 #1
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
样例输出 #1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1
提示
对于 100% 的数据,$1 \le n \le 30 $ 。
这题就是BFS了
我就是先把找到的位置染上色然后输出的时候按照颜色输出就好了
AC

#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
int mp[105][105];
int x[] = {-1,1,0,0};
int y[] = {0,0,-1,1};
void dfs(int a,int b)
{
    for(int i = 0;i < 4;i++)
    {
        int cx = a + x[i],cy = b + y[i];
        if(cx > n + 1 || cx < 0|| cy > n + 1 || cy < 0 ||mp[cx][cy] == 1||mp[cx][cy] == 2)
        {
            continue;
        }
        mp[cx][cy] = 2;
        dfs(cx,cy);
    }
}
int main()
{
    cin>>n;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            cin>>mp[i][j];
        }
    }
    dfs(0,0);
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            if(mp[i][j] == 2)
            {
                cout<<0<<" ";
            }
            else if(mp[i][j] == 0)
            {
                cout<<2<<" ";
            }
            else
            {
                cout<<1<<" ";
            }
        }
        cout<<'\n';
    }
    return 0;
}

最后,制作不易,点赞。


8 :30 开始考试

8:40 快速浏览了T1并想了一下,是一道质数的题目,准备打表,打到一半的时候发现空间复杂度会爆,于是改打质数筛暴力了

9:30打完T1开始看T2刚开始没思路,先看了T3,跟着样例打了一点,估计可以拿点分吧

9:50打完了T3会看T2发现了一点规律(后来知道是错的)跟着思路写了一点。

10:10看T4了想了大概10分钟的样子,觉得还行就写了个暴力

10:40+ 写完了T4,开始检查了

预估分数

100 + 30 + 30 + 0 =160

实际分数 

30 + 0 + 50 + 0 = 80

赛后总结

啊啊啊啊啊,别人是10年OI一场空,不开long long一场空,为什么我是开了long long 一场空啊啊啊啊啊啊啊我要是第一题不开long long就100了啊啊啊啊,其他的都是我思路没想对,下次加油吧。

题解
1.  镜面质数
题目ID:20313必做题100分

时间限制: 1000ms

空间限制: 262144kB

题目描述
如果一个质数镜像反转(即将该数各个位上数字反转)后仍然是质数,那么就称这个质数为镜像质数。例如质数1313反转后为3131,则1313为一个镜像质数。
现在给定一个整数 N,请求出整数1∼ N范围内有几个镜像质数。
注意:求范围内的镜像质数时,质数和镜像反转后的数都需在范围内。详见样例1解释。

输入格式
一个整数 N。

输出格式
一个整数,表示整数1∼ N范围内镜像质数的个数。

样例
Input 1
20

Output 1
5

Input 2
100000

Output 2
1759

样例解释

针对样例1:1∼ 201∼ 20内镜像质数有: {2,3,5,7,11}{2,3,5,7,11},结果为55。
注意:质数1313和1717反转后不在范围1∼ 201∼ 20内,不算入此范围内的镜像质数

数据范围
对于 30%30% 的数据,1≤N≤103;
对于 50%50% 的数据,1≤N≤106;
对于 100%100% 的数据,1≤N≤5×107。

这是一道反转数加上质数筛的题目

首先我们先写一个质数筛

inline void init()
{
    st[1] = true;
    for (int i = 2; i <= n; i++)
    {
        if (!st[i])
        {
            ar[++idx] = i;
        }
        for (int j = 1; j <= idx; j++)
        {
            if (i * ar[j] > n)
            {
                break;
            }
            st[i * ar[j]] = true;
            if (i % ar[j] == 0)
            {
                break;
            }
        }
    }
}

再在主函数里加上判断反数合不合法的函数就信了

int res = 0;
    for (int i = 1; i <= idx; i++)
    {
        int z = ar[i];
        int temp = 0;
        while (z)
        {
            temp = temp * 10 + z % 10;
            z /= 10;
        }
        if (temp <= n && !st[temp])
        {
            res++;
        }
    }
最后提醒大家:不要无脑开long long不然向我一样爆0两行泪!!!!!

附上ACcode

#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N = 5e7 + 10;
int ar[N], idx, n;
bool st[N];
inline void init()
{
    st[1] = true;
    for (int i = 2; i <= n; i++)
    {
        if (!st[i])
        {
            ar[++idx] = i;
        }
        for (int j = 1; j <= idx; j++)
        {
            if (i * ar[j] > n)
            {
                break;
            }
            st[i * ar[j]] = true;
            if (i % ar[j] == 0)
            {
                break;
            }
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    init();
    int res = 0;
    for (int i = 1; i <= idx; i++)
    {
        int z = ar[i];
        int temp = 0;
        while (z)
        {
            temp = temp * 10 + z % 10;
            z /= 10;
        }
        if (temp <= n && !st[temp])
        {
            res++;
        }
    }
    cout << res << "\n";
    return 0;
}

2.  百万富翁的第二次实验
题目ID:20307必做题100分

时间限制: 1000ms

空间限制: 524288kB

题目描述
题目描述
马克吐温有一本非常著名的小说《百万英镑》,这本小说中主角最后归还了百万英镑给两位富翁。但结果就是两位富翁依然有无穷的问题需要进行社会实验,于是,他们打算进行第二次社会实验。那就是不同财富值的人在一场舞会上会发生什么事情。为了满足自己的好奇,百万富翁们邀请了全伦敦所有人来自己的舞会。舞会开始后他们就后悔了,因为来的人太多了,而且很多人的财富都相同,统计起来太费事了。所以百万富翁们找到你,希望你根据来舞会的时间,找出在一段时间内,来舞会的所有人财富值都互不相同的人数。

输入格式
第一行输入一个n表示有n个人参与舞会。

按照时间顺序输入n个人的财富值。

输出格式
输出在一段时间内参加舞会的所有人财富值都互不相同的人数的最大值。

样例
Input 1
Output 1
4

数据范围
每个人的财富值不超过100000000000

0 <= n <= 1000000

这道题看着有点玄乎,其实理一理就会发现很简单

首先看样例

7 2 3 4 5 5 6 7
先边里到第一个数7,发现没有和7一样的数,往下遍历

7 2 3 4 5 5 6 7
没有发现一样的,往下

7 2 3 4 5 5 6 7
没有一样的,往下

7 2 3 4 5 5 6 7
往下

7 2 3 4 5 5 6 7
在往下就发现一样的了

于是就吧前面的删掉

7 2 3 4 5 5 6 7
于是就得到了4

所以样例是这么得来的,根据这个思路往下写就可以A了。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
map<int, int>mp;
int ar[N], n, res;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1, l = 0; i <= n; i++) 
    {
        cin >> ar[i];
        if (mp[ar[i]] > 0)
        {
            int z = mp[ar[i]];
            for (; l <= z; l++)
            {
                mp[ar[l]] = 0;
            }
            l--;
        }
        mp[ar[i]] = i;
        res = max(res, i - l);
    }
    cout << res << "\n";
    return 0;
}

这题就有1e9的大小了,所以要开long long

3.  蛋滚派对
题目ID:20297必做题100分

最新提交:

Accepted

100 分历史最高:

Accepted

100 分

时间限制: 2000ms

空间限制: 524288kB

题目描述
“趴着好难受啊蛋!我要透不过气了蛋!”

“我也不想仰着睡啊蛋!!!”

“那咱俩都翻过来睡呗蛋!”

n 个蛋蛋排成一排睡觉,有些蛋蛋喜欢趴着睡,有些则喜欢仰着睡。你可以做任意次操作,使得相邻的两个蛋蛋都翻过来睡。它们最开始都有个愉悦值,对于任何一个蛋蛋,翻过来之后愉悦值都会变成原来的相反数。

请你最大化它们的愉悦值之和,并回答这个值。

注意:第 11 个蛋蛋和第 n 个蛋蛋不相邻!

输入格式
第一行一个正整数 n,表示蛋蛋的数量。

第二行 n 个整数,其中第 i 个整数 ai​ 表示第 i 个蛋蛋的初始愉悦值。

输出格式
一个整数,表示操作后的最大愉悦值之和。

样例
Input 1
3 -10 5 -4

Output 1
19

Input 2
5 10 -4 -8 -11 3

Output 2
30

Input 3
11 -1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Output 3
10000000000

样例解释
样例一中,1,21,2 翻滚,2,32,3 翻滚,最后愉悦值为 {10,5,4}{10,5,4},总和为 1919。

样例二中,2,32,3 翻滚,4,54,5 翻滚,最后愉悦值为 {10,4,8,11,−3}{10,4,8,11,−3},总和为 3030。

数据范围
对于数据 1∼41∼4 :1≤n≤4000

对于数据 5∼205∼20 :1≤≤n≤105

对于所有数据:−109≤ai​≤109

这道题本来是想骗个分的,结果没骗到,后来改了就A了

我是想有0的情况直接输出sum,没有的话再abs一下求出来,结果sum加时写错了不然A了。。。

没有什么好说的,上代码

#include <bits/stdc++.h>
using namespace std;
long long  a[114514], minx = 1145141919810, sum = 0, sumf;
signed main()
{
    bool uuu = 0;
    long long  sumf = 0;
    long long  n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum += abs(a[i]);
        if (a[i] < 0)
        {
            sumf++;
        }
        if (a[i] == 0)
        {
            uuu = 1;
        }
        minx = min(abs(a[i]), minx);
    }
    if (uuu)
    {
        cout << sum;
        return 0;
    }
    if (sumf % 2 == 0)
    {
        cout << sum;
        return 0;
    }
    cout << sum - minx * 2;
    return 0;
}

最后就是我们亲(tao)爱(yan)的T4了

4.  小信的鸡舍
题目ID:20314必做题100分

时间限制: 1000ms

空间限制: 262144kB

题目描述
小信的Stardew农场是一个N×N的矩阵,由于农场水土流失严重,每个单位土地的高度都不同,第i行第j列的土地高度为Ai,j​。

小信想在农场里选一块K×K的土地来建造他的鸡舍,但他是一个极致的完美主义者,每个K×K的区域中都有K2个土地高度,他希望在所有的K×K区域中选择土地高度中位数最小的那一块区域来作为鸡舍的建造位置。

小信认为K×K区域中K2个土地高度应该是第(⌊2K2​⌋+1)高的数,但他笨手笨脚,他想知道建造鸡舍的土地高度最小的中位数是多少,他需要你的帮助!

输入格式
第一行输入两个整数N和K(1≤K≤N≤800),分别表示农场的长宽和建造鸡舍的长宽。

接下来N行,每行N个整数Ai,j​,表示第i行第j列的土地高度为,j​(0≤Ai,j​≤109)。

输出格式
输出一个整数,表示所有K×K区域中的土地高度最小中位数。

样例
Input 1
3 2 1 7 0 5 8 11 10 4 2

Output 1
4

Input 2
3 3 1 2 3 4 5 6 7 8 9

Output 2
5

样例解释
对于样例#1,(i,j)表示第i行第j列,可以选择的2×22×2区域为:{(1,1),(1,2),(2,1),(2,2)},{(1,2),(1,3),(2,2),(2,3)},{(2,1),(2,2),(3,1),(3,2)},{(2,2),(2,3),(3,2),(3,3)}{(1,1),(1,2),(2,1),(2,2)},{(1,2),(1,3),(2,2),(2,3)},{(2,1),(2,2),(3,1),(3,2)},{(2,2),(2,3),(3,2),(3,3)},2K=2,则中位数应为第(⌊222⌋+1=3)(⌊222​⌋+1=3)个数,四个区域的中位数分别为:5,7,5,45,7,5,4,所以小信应该选择第四个区域来建造他的鸡舍。

数据范围
对于10%10%的数据,1≤101≤K≤N≤10,0≤1000≤Ai,j​≤100。

对于20%20%的数据,1≤601≤K≤N≤60,0≤1090≤Ai,j​≤109。

对于100%100%的数据,1≤8001≤K≤N≤800,0≤1090≤Ai,j​≤109。

这题嘛,

就是二分求中数,但是我好像没有怎么听懂

磨了1个小时终于打出来了但是还是没有听太懂,有人再来给我讲讲嘛

AC代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 10;
map<int, int>mp;
int ar[N][N], n, res, k, temp;
int br[N][N];
inline bool check(int mid)
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            br[i][j] = ar[i][j] <= mid ? 1 : 0;
            br[i][j] = br[i][j] + br[i - 1][j] + br[i][j - 1] - br[i - 1][j - 1];
            if (i >= k && j >= k)
            {
                int z = br[i][j] - br[i - k][j] - br[i][j - k] + br[i - k][j - k];
                if (z >= temp)
                {
                    return true;
                }
            }
        }
    }
    return false;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    temp = (k * k + 1) / 2;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> ar[i][j];
        }
    }
    int l = 0, r = 1e9;
    while (l < r) 
    {
        int mid = l + r >> 1;
        if (check(mid))r = mid;
        else l = mid + 1;
    }
    cout << l << '\n';
    return 0;
}

 这就是我今天的比赛总结了,最后,祝大家学业有成,金榜题名

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值