Educational Codeforces Round 19 A, B, C, E(xjb)
题目链接:http://codeforces.com/contest/797
A题
题意:给出两个数n, k,问能不能将n分解成k个因子相乘的形式,不能输出-1,能则输出其因子;
思路:将n质因分解,若质因子数目对于k则可行,随便将其组合成k个因子即可,反之则不行;
代码:
#include <bits/stdc++.h>
using namespace std; const int MAXN=1e5;
int a[MAXN]; int main(void){
int n, k, indx=;
cin >> n >> k;
for(int i=; i<=sqrt(n); i++){
while(n%i==){
a[indx++]=i;
n/=i;
}
}
if(n>) a[indx++]=n;
if(indx<k){
cout << - << endl;
}else{
for(int i=; i<k-; i++){
cout << a[i] << " ";
}
int cnt=;
for(int i=k-; i<indx; i++){
cnt*=a[i];
}
cout << cnt << endl;
}
return ;
}
B题
题意:给出一个含有n个元素的数组,求其子序列的和最大且为奇数,输出其和;
思路:先求出所有正数的和,若为奇数直接输出即可,不然则再加一个最大的负数并使其为奇数或减去一个最小的奇数;
代码:
#include <bits/stdc++.h>
using namespace std; const int MAXN=1e5+;
int a[MAXN]; int main(void){
int n, cnt=MAXN;
cin >> n;
for(int i=; i<n; i++){
cin >> a[i];
int gg=abs(a[i]);
if((gg&)&&gg<cnt){
cnt=gg;
}
}
sort(a, a+n);
int ans=, indx=n-;
while(a[indx]>){
ans+=a[indx];
indx--;
}
if(ans&){
cout << ans << endl;
}else{
cout << ans-cnt << endl;
}
return ;
}
C题
题意:给出一个字符串,从第一关字符开始去s中字符放入t数组中,可以从任意时刻从t末尾取字符放入u中,问u可能的最小字典序排列;
思路:显然这个过程就是栈的操作过程,那么只需要用栈模拟一下这题即可;
先预处理一个数组str, str[i]存储从s末尾字符到第i个字符中的最小字符;
再遍历一遍s字符串,对于当前地 i 个字符,维护顶元素>str[i],最终得到的出栈顺序即为答案;
代码:
#include <bits/stdc++.h>
using namespace std; const int MAXN=1e5+;
stack<int> st;
int str[MAXN]; int main(void){
string s;
cin >> s;
int len=s.size();
str[len]=;
for(int i=len-; i>=; i--){
str[i]=min(str[i+], s[i]-'a');
}
int indx=;
for(int i=; i<len; i++){
while(!st.empty()){
int cnt=st.top();
if(cnt<=str[i]){
cout << (char)(cnt+'a');
st.pop();
}else break;
}
st.push(s[i]-'a');
}
while(!st.empty()){
int cnt=st.top();
st.pop();
cout << (char)(cnt+'a');
}
cout << endl;
return ;
}
D题:看懂题目再补。。。
E题
题意:有一个含n个元素的数组,接下来有q个查询,每组给出两个数p, k;
对于每组查询输出达到题目要求的最小步数,要求为:p=k+p+a[p],迭代到p>n为止;
思路:要是直接暴力时间复杂度为O(q*n)肯定会tle的,不过稍微加点技巧就好了;
先对500以内的k打下表,对于k>500的数,p每次都加k,所以最多500次就好了,那么时间复杂度只有O(500*q);
代码:
#include <bits/stdc++.h>
using namespace std; const int MAXN=1e5+;
int a[MAXN], vis[][MAXN]; int main(void){
int n, q, p, k;
scanf("%d", &n);
for(int i=; i<=n; i++){
scanf("%d", &a[i]);
}
for(int i=; i<=; i++){
for(int j=n; j>; j--){
vis[i][j]=i+j+a[j]>n?:vis[i][a[j]+j+i]+;
}
}
scanf("%d", &q);
while(q--){
scanf("%d%d", &p, &k);
if(k<=) printf("%d\n", vis[k][p]);
else{
int ans=;
while(p<=n){
p+=a[p]+k;
ans++;
}
printf("%d\n", ans);
}
}
return ;
}
Educational Codeforces Round 19 A, B, C, E(xjb)的更多相关文章
- Educational Codeforces Round 19
A. k-Factorization 题目大意:给一个数n,求k个大于1的数,乘积为n.(n<=100,000,k<=20) 思路:分解质因数呗 #include<cstdio> ...
- Educational Codeforces Round 19 题解【ABCDE】
A. k-Factorization 题意:给你一个n,问你这个数能否分割成k个大于1的数的乘积. 题解:因为n的取值范围很小,所以感觉dfs应该不会有很多种可能-- #include<bits ...
- 【Educational Codeforces Round 19】
这场edu蛮简单的…… 连道数据结构题都没有…… A.随便质因数分解凑一下即可. #include<bits/stdc++.h> #define N 100005 using namesp ...
- Educational Codeforces Round 19 A+B+C+E!
A. k-Factorization 题意:将n分解成k个大于1的数相乘的形式.如果无法分解输出-1. 思路:先打个素因子表,然后暴力判,注意最后跳出的条件. int len,a[N],b[N]; v ...
- Educational Codeforces Round 19 C
Description Petya recieved a gift of a string s with length up to 105 characters for his birthday. H ...
- Educational Codeforces Round 19 B
Description You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to f ...
- Educational Codeforces Round 19 A
Description Given a positive integer n, find k integers (not necessary distinct) such that all these ...
- Educational Codeforces Round 19 E. Array Queries(暴力)(DP)
传送门 题意 给出n个数,q个询问,每个询问有两个数p,k,询问p+k+a[p]操作几次后超过n 分析 分块处理,在k<sqrt(n)时,用dp,大于sqrt(n)用暴力 trick 代码 #i ...
- Educational Codeforces Round 17
Educational Codeforces Round 17 A. k-th divisor 水题,把所有因子找出来排序然后找第\(k\)大 view code //#pragma GCC opti ...
随机推荐
- jQuery remove()与jQuery empty()的区别
jQuery remove() 方法删除被选元素及其子元素.举例如下: <!DOCTYPE html> <html> <head> <script src=& ...
- 有关Option.inSamplSize 和 Compress 图片压缩
1.图片占用内存:占用的内存 = 图片长度 * 图片宽度 * 单位像素占用的字节数 注: 图片长度和图片宽度就是图片在行列上的像素数量. 图片格式: ALPHA_8:表示8位Alpha位图,即A=8, ...
- 03-树1 树的同构(25 point(s)) 【Tree】
03-树1 树的同构(25 point(s)) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为 ...
- Java内存模型(JMM)中的happens-before
happens-before是JMM中最核心的概念,对于Java程序员来说,理解happens-before是理解JMM的关键 . 1.JMM的设计 首先,来看看JMM的设计意图.从JMM的设计者的角 ...
- hdu 1361.Parencodings 解题报告
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence. P-se ...
- 编译Thrift
按照 https://syslint.com/blog/tutorial/how-to-install-apache-thrift-on-ubuntu-14-04/ 进行, 编译时出现错误 make[ ...
- 并不对劲的CTS2019
day0 没有C day1 t1:并不想简述题意 10分暴力走人 t2:有\(n\)个在\([1,D]\)内的均匀随机整数,问有多少的概率出现\(m\)对相同的 设\(f(i,j)\)表示考虑前\(i ...
- linux文件查找(find,locate)
文件查找: locate: 非实时,模糊匹配,查找是根据全系统文件数据库进行的: # updatedb, 手动生成文件数据库 速度快 find: 实时 精确 ...
- 「NOIP2002」「Codevs1099」 字串变换(BFS
1099 字串变换 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知有两个字串 $A$, ...
- Exceprtion:e createQuery is not valid without active transaction; nested exception is org.hibernate.HibernateException: createQuery is not valid without active transaction
如果增加配置了current_session_context_class属性,查询的时候需要session.beginTrasaction()来开启事务