codeforces每日5题(均1600)-第三十三天

2023-11-08 11:59

本文主要是介绍codeforces每日5题(均1600)-第三十三天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Alyona and Spreadsheet

题面翻译

给出一个n*m的矩阵

对于第j列,如果满足 ∀ i ∈ [ 1 , n − 1 ] , a i , j ≤ a i + 1 , j \forall i \in [1,n-1],a_{i,j} \leq a_{i+1,j} i[1,n1],ai,jai+1,j,则称这一列是不下降的。

k次询问,问如果只保留矩阵的第L~R行,矩阵中是否存在不下降的一列。

题目描述

During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.

Now she has a table filled with integers.

The table consists of n n n rows and m m m columns.

By a i , j a_{i,j} ai,j we will denote the integer located at the i i i -th row and the j j j -th column.

We say that the table is sorted in non-decreasing order in the column j j j if a i , j < = a i + 1 , j a_{i,j}<=a_{i+1,j} ai,j<=ai+1,j for all i i i from 1 1 1 to n − 1 n-1 n1 .

Teacher gave Alyona k k k tasks.

For each of the tasks two integers l l l and r r r are given and Alyona has to answer the following question: if one keeps the rows from l l l to r r r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column?

Formally, does there exist such j j j that a i , j < = a i + 1 , j a_{i,j}<=a_{i+1,j} ai,j<=ai+1,j for all i i i from l l l to r − 1 r-1 r1 inclusive.

Alyona is too small to deal with this task and asks you to help!

输入格式

The first line of the input contains two positive integers n n n and m m m ( 1 < = n ⋅ m < = 100000 1<=n·m<=100000 1<=nm<=100000 ) — the number of rows and the number of columns in the table respectively.

Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.

Each of the following n n n lines contains m m m integers.

The j j j -th integers in the i i i of these lines stands for a i , j a_{i,j} ai,j ( 1 < = a i , j < = 1 0 9 1<=a_{i,j}<=10^{9} 1<=ai,j<=109 ).

The next line of the input contains an integer k k k ( 1 < = k < = 100000 1<=k<=100000 1<=k<=100000 ) — the number of task that teacher gave to Alyona.

The i i i -th of the next k k k lines contains two integers l i l_{i} li and r i r_{i} ri ( 1 < = l i < = r i < = n 1<=l_{i}<=r_{i}<=n 1<=li<=ri<=n ).

输出格式

Print “Yes” to the i i i -th line of the output if the table consisting of rows from l i l_{i} li to r i r_{i} ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print “No”.

样例 #1

样例输入 #1

5 4
1 2 3 5
3 1 3 2
4 5 2 3
5 5 3 2
4 4 3 4
6
1 1
2 5
4 5
3 5
1 3
1 5

样例输出 #1

Yes
No
Yes
Yes
Yes
No

提示

In the sample, the whole table is not sorted in any column.

However, rows 1–3 are sorted in column 1 1 1 , while rows 4–5 are sorted in column 3 3 3 .

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
int n,m,k,last[N],reach[N],lastre[N],ans[N],kk,l,r;
int main(){memset(ans,0x3f,sizeof(ans));memset(last,0x3f,sizeof(last));cin>>n>>m;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){scanf("%d",&k);if(k<last[j]) reach[j]=i;else reach[j]=lastre[j];lastre[j]=reach[j];last[j]=k;ans[i]=min(ans[i],reach[j]);}}cin>>kk;while(kk--){scanf("%d%d",&l,&r);cout<<(ans[r]<=l?"Yes":"No")<<endl;}return 0;
}

Timofey and a tree

题面翻译

一棵无根树中各个节点被染上了一种颜色c[i]

现在让你选择一个点作为根节点,使得这个根节点的所有儿子满足以该儿子节点的作为根的子树中所有点颜色均相同(不同儿子为根的子树颜色可以不同)。

题目描述

Each New Year Timofey and his friends cut down a tree of n n n vertices and bring it home.

After that they paint all the n n n its vertices, so that the i i i -th vertex gets color c i c_{i} ci .

Now it’s time for Timofey birthday, and his mother asked him to remove the tree.

Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex.

After that Timofey brings the tree to a trash can.

Timofey doesn’t like it when many colors are mixing together.

A subtree annoys him if there are vertices of different color in it.

Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him.

He doesn’t consider the whole tree as a subtree since he can’t see the color of the root vertex.

A subtree of some vertex is a subgraph containing that vertex and all its descendants.

Your task is to determine if there is a vertex, taking which in hands Timofey wouldn’t be annoyed.

输入格式

The first line contains single integer n n n ( 2 < = n < = 1 0 5 2<=n<=10^{5} 2<=n<=105 ) — the number of vertices in the tree.

Each of the next n − 1 n-1 n1 lines contains two integers u u u and v v v ( 1 < = u , v < = n 1<=u,v<=n 1<=u,v<=n , u ≠ v u≠v u=v ), denoting there is an edge between vertices u u u and v v v .

It is guaranteed that the given graph is a tree.

The next line contains n n n integers c 1 , c 2 , . . . , c n c_{1},c_{2},...,c_{n} c1,c2,...,cn ( 1 < = c i < = 1 0 5 1<=c_{i}<=10^{5} 1<=ci<=105 ), denoting the colors of the vertices.

输出格式

Print “NO” in a single line, if Timofey can’t take the tree in such a way that it doesn’t annoy him.

Otherwise print “YES” in the first line.

In the second line print the index of the vertex which Timofey should take in hands.

If there are multiple answers, print any of them.

样例 #1

样例输入 #1

4
1 2
2 3
3 4
1 2 1 1

样例输出 #1

YES
2

样例 #2

样例输入 #2

3
1 2
2 3
1 2 3

样例输出 #2

YES
2

样例 #3

样例输入 #3

4
1 2
2 3
3 4
1 2 1 2

样例输出 #3

NO
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
int n,a[N],b[N],c[N],k=0,s[N];
int main(){scanf("%d",&n);for(int i=1;i<=n-1;i++) scanf("%d%d",&a[i],&b[i]);for(int i=1;i<=n;i++) scanf("%d",&c[i]);for(int i=1;i<=n-1;i++){if(c[a[i]]!=c[b[i]]){k++;s[a[i]]++,s[b[i]]++;}}for(int i=1;i<=n;i++){if(s[i]==k){printf("YES\n%d\n",i);return 0;}}puts("NO");return 0;
}

An impassioned circulation of affection

题面翻译

给你一个由小写字母构成的字符串.  
有$q$个询问,每个询问给出数字$m$和小写字母$c$,你可以任意地修改字符串中的$m$个字符,求最多能够使字符串中含有多少个连续相同的字母$c$.  
每个询问各自独立.

题目描述

Nadeko’s birthday is approaching!

As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall.

Brother Koyomi will like it!

Still unsatisfied with the garland, Nadeko decided to polish it again.

The garland has n n n pieces numbered from 1 1 1 to n n n from left to right, and the i i i -th piece has a colour s i s_{i} si , denoted by a lowercase English letter.

Nadeko will repaint at most m m m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter).

After this work, she finds out all subsegments of the garland containing pieces of only colour c c c — Brother Koyomi’s favourite one, and takes the length of the longest among them to be the Koyomity of the garland.

For instance, let’s say the garland is represented by “kooomo”, and Brother Koyomi’s favourite colour is “o”.

Among all subsegments containing pieces of “o” only, “ooo” is the longest, with a length of 3 3 3 .

Thus the Koyomity of this garland equals 3 3 3 .

But problem arises as Nadeko is unsure about Brother Koyomi’s favourite colour, and has swaying ideas on the amount of work to do.

She has q q q plans on this, each of which can be expressed as a pair of an integer m i m_{i} mi and a lowercase letter c i c_{i} ci , meanings of which are explained above.

You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.

输入格式

The first line of input contains a positive integer n n n ( 1 < = n < = 1500 1<=n<=1500 1<=n<=1500 ) — the length of the garland.

The second line contains n n n lowercase English letters s 1 s 2 . . . s n s_{1}s_{2}...\ s_{n} s1s2... sn as a string — the initial colours of paper pieces on the garland.

The third line contains a positive integer q q q ( 1 < = q < = 200000 1<=q<=200000 1<=q<=200000 ) — the number of plans Nadeko has.

The next q q q lines describe one plan each: the i i i -th among them contains an integer m i m_{i} mi ( 1 < = m i < = n 1<=m_{i}<=n 1<=mi<=n ) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c i c_{i} ci — Koyomi’s possible favourite colour.

输出格式

Output q q q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.

样例 #1

样例输入 #1

6
koyomi
3
1 o
4 o
4 m

样例输出 #1

3
6
5

样例 #2

样例输入 #2

15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b

样例输出 #2

3
4
5
7
8
1
2
3
4
5

样例 #3

样例输入 #3

10
aaaaaaaaaa
2
10 b
10 z

样例输出 #3

10
10

提示

In the first sample, there are three plans:

  • In the first plan, at most 1 1 1 piece can be repainted.

Repainting the “y” piece to become “o” results in “kooomi”, whose Koyomity of 3 3 3 is the best achievable;

  • In the second plan, at most 4 4 4 pieces can be repainted, and “oooooo” results in a Koyomity of 6 6 6 ;
  • In the third plan, at most 4 4 4 pieces can be repainted, and “mmmmmi” and “kmmmmm” both result in a Koyomity of 5 5 5 .
#include<bits/stdc++.h>
using namespace std;
const int N=2e3+10;
typedef long long ll;
int n,m,k,p[N][27];
char s[1600];
char ch;
int main(){scanf("%d",&n);scanf("%s",s+1);cin>>m;for(register int i=1;i<=n;i++){for(register int j=0;j<26;j++){p[i][j]+=p[i-1][j];}++p[i][s[i]-'a'];}for(register int i=1;i<=m;i++){int l=1,r=1,ans=0;scanf("%d %c",&k,&ch);while(r<=n){while(p[r][ch-'a']-p[l-1][ch-'a']+k<r-l+1) ++l;ans=max(ans,r-l+1);++r;}printf("%d\n",ans);}return 0;
}

Multi-judge Solving

题面翻译

DecoForces 上存在 n n n 个问题,难度为 a 1 , a 2 , a 3 ⋯ a n a_1,a_2,a_3\cdots a_n a1,a2,a3an,你已经做到了难度最高为 k k k 的题目。

现在存在两种 OJ,一个是上述的 DecoForces,一个是其他的 OJ。如果 DecoForces 没有某一难度的题目,那其他 OJ 上一定有。

假设你现在做到了难度为 k k k 的题目,那你下次可以做 a i ≤ 2 × k a_i\le 2\times k ai2×k 的题目,做完之后 k k k 就要更新为你做过最难的题目。

你至少要在其他 OJ 上做几道题?

Translate By Aw顿顿

题目描述

Makes solves problems on Decoforces and lots of other different online judges.

Each problem is denoted by its difficulty — a positive integer number.

Difficulties are measured the same across all the judges (the problem with difficulty d d d on Decoforces is as hard as the problem with difficulty $ d $ on any other judge).

Makes has chosen n n n problems to solve on Decoforces with difficulties a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an .

He can solve these problems in arbitrary order.

Though he can solve problem i i i with difficulty a i a_{i} ai only if he had already solved some problem with difficulty (no matter on what online judge was it).

Before starting this chosen list of problems, Makes has already solved problems with maximum difficulty k k k .

With given conditions it’s easy to see that Makes sometimes can’t solve all the chosen problems, no matter what order he chooses.

So he wants to solve some problems on other judges to finish solving problems from his list.

For every positive integer y y y there exist some problem with difficulty y y y on at least one judge besides Decoforces.

Makes can solve problems on any judge at any time, it isn’t necessary to do problems from the chosen list one right after another.

Makes doesn’t have too much free time, so he asked you to calculate the minimum number of problems he should solve on other judges in order to solve all the chosen problems from Decoforces.

输入格式

The first line contains two integer numbers n n n , k k k ( 1 < = n < = 1 0 3 1<=n<=10^{3} 1<=n<=103 , 1 < = k < = 1 0 9 1<=k<=10^{9} 1<=k<=109 ).

The second line contains n n n space-separated integer numbers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an ( 1 < = a i < = 1 0 9 1<=a_{i}<=10^{9} 1<=ai<=109 ).

输出格式

Print minimum number of problems Makes should solve on other judges in order to solve all chosen problems on Decoforces.

样例 #1

样例输入 #1

3 3
2 1 9

样例输出 #1

1

样例 #2

样例输入 #2

4 20
10 3 6 3

样例输出 #2

0

提示

In the first example Makes at first solves problems 1 and 2.

Then in order to solve the problem with difficulty 9, he should solve problem with difficulty no less than 5.

The only available are difficulties 5 and 6 on some other judge. Solving any of these will give Makes opportunity to solve problem 3.

In the second example he can solve every problem right from the start.

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
ll n,k,a[N],ans=0;
int main(){cin>>n>>k;for(int i=1;i<=n;i++) scanf("%lld",&a[i]);sort(a+1,a+1+n);for(int i=1;i<=n;i++){while(a[i]>2*k) ans++,k*=2;k=max(k,a[i]);}cout<<ans<<endl;return 0;
}

Preparing for Merge Sort

题面翻译

Ivan有一个包含 n n n个不同整数的数组。他计划将这个数组变成升序的。Ivan喜欢归并排序,所以他想将这个数组变成一个或多个升序数组,之后将它们合并。

他用如下的方式将原数组变成一个或多个升序数组:

Ivan将对数组进行若干次迭代,直到数组中所有元素都被放入新数组。

对于每次迭代,Ivan将依次从左到右遍历每个还未放入新数组中的元素。如果某个元素是该次迭代中的第一个元素,那么它将会放入属于本次迭代的新数组中。如果某个元素不是该次迭代中的第一个元素,那么当且仅当它比属于本次迭代的新数组中最后一个数大时,它将被放入属于本次迭代的新数组的末尾。

更具体的,对于一串数 [ 1 , 3 , 2 , 5 , 4 ] [1,3,2,5,4] [1,3,2,5,4],第一次迭代将取出 [ 1 , 3 , 5 ] [1,3,5] [1,3,5] 3 3 3个元素,第二次迭代将取出 [ 2 , 4 ] [2,4] [2,4] 2 2 2个元素,因为它们是严格递增的。

输入格式:第一行一个数 n ( n ≤ 2 × 1 0 5 ) n(n\leq2\times 10^5) n(n2×105),表示数组中元素的个数。第二行 n n n个整数,表示数组中的每个元素。

输出格式:每行若干个数,第 i i i行表示属于第 i ​ i​ i次迭代的新数组。

题目描述

Ivan has an array consisting of n n n different integers.

He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.

Ivan represent his array with increasing sequences with help of the following algorithm.

While there is at least one unused number in array Ivan repeats the following procedure:

  • iterate through array from the left to the right;
  • Ivan only looks at unused numbers on current iteration;
  • if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.

For example, if Ivan’s array looks like [1, 3, 2, 5, 4] then he will perform two iterations.

On first iteration Ivan will use and write numbers [1, 3, 5], and on second one — [2, 4].

Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.

输入格式

The first line contains a single integer n KaTeX parse error: Expected '}', got 'EOF' at end of input: ( 1<=n<=2·10^{ ) — the number of elements in Ivan’s array.

The second line contains a sequence consisting of distinct integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an ( 1 < = a i < = 1 0 9 1<=a_{i}<=10^{9} 1<=ai<=109 ) — Ivan’s array.

输出格式

Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.

样例 #1

样例输入 #1

5
1 3 2 5 4

样例输出 #1

1 3 5 
2 4

样例 #2

样例输入 #2

4
4 3 2 1

样例输出 #2

4 
3 
2 
1

样例 #3

样例输入 #3

4
10 30 50 101

样例输出 #3

10 30 50 101
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N=1e6+10;
typedef long long ll;
int n,k,p,a[N];
vector<int> v[N];
int main(){cin>>n;for(int i=1;i<=n;i++){scanf("%d",&k);p=lower_bound(a,a+n+1,k)-a-1;a[p]=k;v[p].push_back(k);}for(int i=n;i>=0&&v[i].size();i--,puts("")){for(int j=0;j<v[i].size();j++){printf("%d ",v[i][j]);}}return 0;
}

这篇关于codeforces每日5题(均1600)-第三十三天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369713

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

每日一题|牛客竞赛|四舍五入|字符串+贪心+模拟

每日一题|四舍五入 四舍五入 心有猛虎,细嗅蔷薇。你好朋友,这里是锅巴的C\C++学习笔记,常言道,不积跬步无以至千里,希望有朝一日我们积累的滴水可以击穿顽石。 四舍五入 题目: 牛牛发明了一种新的四舍五入应用于整数,对个位四舍五入,规则如下 12345->12350 12399->12400 输入描述: 输入一个整数n(0<=n<=109 ) 输出描述: 输出一个整数

每日一练7:简写单词(含链接)

1.链接 简写单词_牛客题霸_牛客网 2.题目 3.代码1(错误经验) #include <iostream>#include <string>using namespace std;int main() {string s;string ret;int count = 0;while(cin >> s)for(auto a : s){if(count == 0){if( a <=

【每日刷题】Day113

【每日刷题】Day113 🥕个人主页:开敲🍉 🔥所属专栏:每日刷题🍍 🌼文章目录🌼 1. 91. 解码方法 - 力扣(LeetCode) 2. LCR 098. 不同路径 - 力扣(LeetCode) 3. 63. 不同路径 II - 力扣(LeetCode) 1. 91. 解码方法 - 力扣(LeetCode) //思路:动态规划。 cl

力扣 739. 每日温度【经典单调栈题目】

1. 题目 理解题意: 1.1. 给一个温度集合, 要返回一个对应长度的结果集合, 这个结果集合里面的元素 i 是 当前 i 位置的元素的下一个更高温度的元素的位置和当前 i 位置的距离之差, 若是当前元素不存在下一个更高温度的元素, 则这个位置用0代替; 2. 思路 本题用单调栈来求解;单调栈就适用于来求当前元素左边或者右边第一个比当前元素大或者小的元素;【单调栈:让栈中的元素保持单调