Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】
A. Prime Subtraction
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two integers x and y (it is guaranteed that x>y). You may choose any prime integer p and subtract it any number of times from x. Is it possible to make x equal to y?
Recall that a prime number is a positive integer that has exactly two positive divisors: 1 and this integer itself. The sequence of prime numbers starts with 2, 3, 5, 7, 11.
Your program should solve t independent test cases.
Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.
Then t lines follow, each describing a test case. Each line contains two integers x and y (1≤y<x≤1018).
Output
For each test case, print YES if it is possible to choose a prime number p and subtract it any number of times from x so that x becomes equal to y. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
Example
inputCopy
4
100 98
42 32
1000000000000000000 1
41 40
outputCopy
YES
YES
YES
NO
Note
In the first test of the example you may choose p=2 and subtract it once.
In the second test of the example you may choose p=5 and subtract it twice. Note that you cannot choose p=7, subtract it, then choose p=3 and subtract it again.
In the third test of the example you may choose p=3 and subtract it 333333333333333333 times.
AC代码:
#include<bits/stdc++.h> using namespace std;
#define int long long signed main(){
int _;
cin>>_;
while(_--){
int n,m;
cin>>n>>m;
int x=n-m;
if(x==){
printf("NO\n");
}else{
printf("YES\n");
}
}
return ;
}
B. Kill 'Em All
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ivan plays an old action game called Heretic. He's stuck on one of the final levels of this game, so he needs some help with killing the monsters.
The main part of the level is a large corridor (so large and narrow that it can be represented as an infinite coordinate line). The corridor is divided into two parts; let's assume that the point x=0 is where these parts meet.
The right part of the corridor is filled with n monsters — for each monster, its initial coordinate xi is given (and since all monsters are in the right part, every xi is positive).
The left part of the corridor is filled with crusher traps. If some monster enters the left part of the corridor or the origin (so, its current coordinate becomes less than or equal to 0), it gets instantly killed by a trap.
The main weapon Ivan uses to kill the monsters is the Phoenix Rod. It can launch a missile that explodes upon impact, obliterating every monster caught in the explosion and throwing all other monsters away from the epicenter. Formally, suppose that Ivan launches a missile so that it explodes in the point c. Then every monster is either killed by explosion or pushed away. Let some monster's current coordinate be y, then:
if c=y, then the monster is killed;
if y<c, then the monster is pushed r units to the left, so its current coordinate becomes y−r;
if y>c, then the monster is pushed r units to the right, so its current coordinate becomes y+r.
Ivan is going to kill the monsters as follows: choose some integer point d and launch a missile into that point, then wait until it explodes and all the monsters which are pushed to the left part of the corridor are killed by crusher traps, then, if at least one monster is still alive, choose another integer point (probably the one that was already used) and launch a missile there, and so on.
What is the minimum number of missiles Ivan has to launch in order to kill all of the monsters? You may assume that every time Ivan fires the Phoenix Rod, he chooses the impact point optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1≤q≤105) — the number of queries.
The first line of each query contains two integers n and r (1≤n,r≤105) — the number of enemies and the distance that the enemies are thrown away from the epicenter of the explosion.
The second line of each query contains n integers xi (1≤xi≤105) — the initial positions of the monsters.
It is guaranteed that sum of all n over all queries does not exceed 105.
Output
For each query print one integer — the minimum number of shots from the Phoenix Rod required to kill all monsters.
Example
inputCopy
2
3 2
1 3 5
4 1
5 2 3 5
outputCopy
2
2
Note
In the first test case, Ivan acts as follows:
choose the point 3, the first monster dies from a crusher trap at the point −1, the second monster dies from the explosion, the third monster is pushed to the point 7;
choose the point 7, the third monster dies from the explosion.
In the second test case, Ivan acts as follows:
choose the point 5, the first and fourth monsters die from the explosion, the second monster is pushed to the point 1, the third monster is pushed to the point 2;
choose the point 2, the first monster dies from a crusher trap at the point 0, the second monster dies from the explosion.
思路:去重,排序,从后往前跑即可。
AC代码:
#include<bits/stdc++.h> using namespace std; #define int long long
bool cmp(int a,int b){
return a>b;
}
int arr[];
signed main(){
int _;
cin>>_;
while(_--){
int n,r;
cin>>n>>r;
for(int i=;i<=n+;i++)
arr[i]=;
int cnt=;
map<int,int> mp;
for(int i=;i<=n;i++)
{
int temp;
scanf("%lld",&temp);
if(mp[temp])
continue;
else{
mp[temp]=;
arr[cnt++]=temp;
}
}
sort(arr+,arr+cnt,cmp);
int ans=;
for(int i=;i<=cnt;i++){
arr[i]-=ans*r;
if(arr[i]>){
ans++;
}else{
break;
}
}
printf("%lld\n",ans);
}
return ;
}
/*
2 3 5
1 2 5
1 2
4 1
1 1 1 1
*/
C. Standard Free2play
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are playing a game where your character should overcome different obstacles. The current problem is to come down from a cliff. The cliff has height h, and there is a moving platform on each height x from 1 to h.
Each platform is either hidden inside the cliff or moved out. At first, there are n moved out platforms on heights p1,p2,…,pn. The platform on height h is moved out (and the character is initially standing there).
If you character is standing on some moved out platform on height x, then he can pull a special lever, which switches the state of two platforms: on height x and x−1. In other words, the platform you are currently standing on will hide in the cliff and the platform one unit below will change it state: it will hide if it was moved out or move out if it was hidden. In the second case, you will safely land on it. Note that this is the only way to move from one platform to another.
Your character is quite fragile, so it can safely fall from the height no more than 2. In other words falling from the platform x to platform x−2 is okay, but falling from x to x−3 (or lower) is certain death.
Sometimes it's not possible to come down from the cliff, but you can always buy (for donate currency) several magic crystals. Each magic crystal can be used to change the state of any single platform (except platform on height h, which is unaffected by the crystals). After being used, the crystal disappears.
What is the minimum number of magic crystal you need to buy to safely land on the 0 ground level?
Input
The first line contains one integer q (1≤q≤100) — the number of queries. Each query contains two lines and is independent of all other queries.
The first line of each query contains two integers h and n (1≤h≤109, 1≤n≤min(h,2⋅105)) — the height of the cliff and the number of moved out platforms.
The second line contains n integers p1,p2,…,pn (h=p1>p2>⋯>pn≥1) — the corresponding moved out platforms in the descending order of their heights.
The sum of n over all queries does not exceed 2⋅105.
Output
For each query print one integer — the minimum number of magic crystals you have to spend to safely come down on the ground level (with height 0).
Example
inputCopy
4
3 2
3 1
8 6
8 7 6 5 3 2
9 6
9 8 5 4 3 1
1 1
1
outputCopy
0
1
2
0
题意有点难懂QAQ。
看看代码吧,有点难讲。
AC代码:
#include<bits/stdc++.h> using namespace std; #define int long long #define N 200050
int arr[N];
signed main(){
int _;
cin>>_;
while(_--){
int n,m;
scanf("%lld%lld",&n,&m);
for(int i=;i<=m;i++)
arr[i]=;
for(int i=;i<=m;i++)
scanf("%lld",&arr[i]);
int ans=;
arr[m+]=;
for(int i=;i<=m;i++)
{
if(arr[i]-==arr[i+]){ //正好能接住。
i++;
}else{
ans++;// 需要一块板接住。
//cout<<i<<endl;
}
}
cout<<ans<<endl;
}
return ;
}
D. AB-string
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The string t1t2…tk is good if each letter of this string belongs to at least one palindrome of length greater than 1.
A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not.
Here are some examples of good strings:
t = AABBB (letters t1, t2 belong to palindrome t1…t2 and letters t3, t4, t5 belong to palindrome t3…t5);
t = ABAA (letters t1, t2, t3 belong to palindrome t1…t3 and letter t4 belongs to palindrome t3…t4);
t = AAAAA (all letters belong to palindrome t1…t5);
You are given a string s of length n, consisting of only letters A and B.
You have to calculate the number of good substrings of string s.
Input
The first line contains one integer n (1≤n≤3⋅105) — the length of the string s.
The second line contains the string s, consisting of letters A and B.
Output
Print one integer — the number of good substrings of string s.
Examples
inputCopy
5
AABBB
outputCopy
6
inputCopy
3
AAA
outputCopy
3
inputCopy
7
AAABABB
outputCopy
15
Note
In the first test case there are six good substrings: s1…s2, s1…s4, s1…s5, s3…s4, s3…s5 and s4…s5.
In the second test case there are three good substrings: s1…s2, s1…s3 and s2…s3.
思路:
AC代码:
#include<bits/stdc++.h> using namespace std;
#define int long long
vector<int> v;
signed main(){
int n;
cin>>n;
string s;
cin>>s;
for(int i=;i<n;i++){
int cnt=;
while(s[i]==s[i+]){
cnt++;
i++;
}
v.push_back(cnt);
}
int ans=n*(n-)/;// 除去单个的字母
int sum=;
for(int i=;i<v.size();i++){
if(i!=){
sum+=v[i-]-;// 除去重复
}
if(i!=v.size()-){
sum+=v[i+];
}
}
int res=ans-sum;
cout<<res<<endl;
return ;
}
Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】的更多相关文章
- Educational Codeforces Round 74 (Rated for Div. 2) D. AB-string
链接: https://codeforces.com/contest/1238/problem/D 题意: The string t1t2-tk is good if each letter of t ...
- Educational Codeforces Round 74 (Rated for Div. 2) C. Standard Free2play
链接: https://codeforces.com/contest/1238/problem/C 题意: You are playing a game where your character sh ...
- Educational Codeforces Round 74 (Rated for Div. 2) B. Kill 'Em All
链接: https://codeforces.com/contest/1238/problem/B 题意: Ivan plays an old action game called Heretic. ...
- Educational Codeforces Round 74 (Rated for Div. 2) A. Prime Subtraction
链接: https://codeforces.com/contest/1238/problem/A 题意: You are given two integers x and y (it is guar ...
- Educational Codeforces Round 74 (Rated for Div. 2)
传送门 A. Prime Subtraction 判断一下是否相差为\(1\)即可. B. Kill 'Em All 随便搞搞. C. Standard Free2play 题意: 现在有一个高度为\ ...
- Educational Codeforces Round 74 (Rated for Div. 2)补题
慢慢来. 题目册 题目 A B C D E F G 状态 √ √ √ √ × ∅ ∅ //√,×,∅ 想法 A. Prime Subtraction res tp A 题意:给定\(x,y(x> ...
- Educational Codeforces Round 74 (Rated for Div. 2)E(状压DP,降低一个m复杂度做法含有集合思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100005];int pos[ ...
- Educational Codeforces Round 33 (Rated for Div. 2) D题 【贪心:前缀和+后缀最值好题】
D. Credit Card Recenlty Luba got a credit card and started to use it. Let's consider n consecutive d ...
- Educational Codeforces Round 77 (Rated for Div. 2)D(二分+贪心)
这题二分下界是0,所以二分写法和以往略有不同,注意考虑所有区间,并且不要死循环... #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> ...
随机推荐
- C语言细节
一些常见细节 int *p[]和 int (*p)[] 的区别 int *p[4]; //定义一个指针数组,该数组中每个元素是一个指针,每个指针指向哪里就需要程序中后续再定义了. int (*p)[4 ...
- 第十六章:网络IPC 套接字
一.IP地址和端口 套接字接口可以用于计算机间通信.目前计算机间使用套接字通讯需要保证处于同一网段. 为了查看是否处于同一网段,我们可以使用IP地址判断. IP地址是计算机在网络中的唯一标识.IP地址 ...
- S03_CH09_DMA_4_Video_Switch视频切换系统
S03_CH09_DMA_4_Video_Switch视频切换系统 9.1概述 本例程详细创建过程和本季课程第一课<S03_CH01_AXI_DMA_LOOP 环路测试>非常类似,因此如果 ...
- SAS学习笔记3 输入输出格式(format、informat函数)
format函数:定义输出格式 informat函数:定义输入格式 proc format:定义输出格式 从外部读取文件 proc format过程步
- 项目遇到的问题:页面c:forEach循环的数据进行计算传回后台并保持到数据库
应该还有更简单的方法 但是我不晓得 手动给文本框输入数据保存到数据库 A表 :通过订单编号 查询数据获得 B表 :通过A表中的字段查询遍历获得 问题: 手动输入文本框内容 保存到数据库 页面form提 ...
- awr报告没有数据11.2.0.3
有个朋友,反馈AWR没有数据: 咨询版本:oracle企业版本11.2.0.3 SQL> select * from v$version; BANNER -------------------- ...
- (五)Maven中的聚合和继承
一.为什么要聚合? 定义:我们在开发过程中,创建了2个以上的模块,每个模块都是一个独立的maven project,在开始的时候我们可以独立的编译和测试运行每个模块,但是随着项目的不断变大和复杂化,我 ...
- SqlServer 多服务器管理配置报错:Ensure the agent startup account for 'x.x.x.x' has rights to login as target server, Access is denied.
SQL Server 2012配置多服务器管理时,SSMS设置一直报错,配置失败: 解决方法: 1. 为SQL Server Agent单独创建一个账号,主服务器和目标服务器都创建一样的账号 2. 把 ...
- 清北学堂-DAY2-数论专题-中国剩余定理(CRT)
首先请看定义:(百科上抄下来的)孙子定理是中国古代求解一次同余式组(见同余)的方法.是数论中一个重要定理.又称中国余数定理. 一元线性同余方程组问题最早可见于中国南北朝时期(公元5世纪)的数学著作&l ...
- element-ui 日期插件让结束日期大于开始日期
<el-date-picker v-model="addForm.startDate" type="date" size="mini" ...