Codeforces_797
学了一学期还是那么菜。
好久没更新,还是得放点东西的。
A.贪心最小的素因子,k = 1时,直接输出n就可以了。
#include<bits/stdc++.h>
using namespace std; int n,k;
vector <int> ans;
int main()
{
ios::sync_with_stdio(false);
cin >> n >> k;
int cnt = ;
if(k == )
{
cout << n <<endl;
return ;
}
for(int i = ;i <= n;i++)
{
while(n%i == )
{
ans.push_back(i);
n /= i;
cnt++;
if(cnt == k- && n > || cnt == k && n == )
{
for(int j = ;j < ans.size();j++) cout << ans[j] << " ";
if(n > ) cout << n;
return ;
}
}
}
cout << - << endl;
return ;
}
B.偶数大于0的全加,奇数排序一下,贪心最大的奇数个数的奇数和。
#include<bits/stdc++.h>
using namespace std; int n,a[]; int main()
{
ios::sync_with_stdio(false);
cin >> n;
long long ans = ;
int cnt = ;
for(int i = ;i <= n;i++)
{
int x;
cin >> x;
if(x% == && x > ) ans += x;
else if(x%) a[++cnt] = x;
}
sort(a+,a++cnt);
ans += a[cnt];
for(int i = cnt-;i >= ;i -= )
{
if(i- < ) break;
if(a[i]+a[i-] > ) ans += a[i]+a[i-];
}
cout << ans << endl;
return ;
}
C.记录出现的字母,起始点为字符串首s,每次操作选下一个出现过的最小的字符c,终点为最后一个该字符的位置e,若s<e,则将该段的字符一个个入栈(等于c的字符直接输出)。另外,每次选取一个字符时,栈顶比当前字符小或相等的字符要输出。
#include<bits/stdc++.h>
using namespace std; string s;
int ok[] = {};
stack<char> ss; int main()
{
ios::sync_with_stdio(false);
cin >> s;
for(int i = ;i < s.length();i++) ok[s[i]] = ;
int now = ;
for(char i = 'a';i <= 'z';i++)
{
if(!ok[i]) continue;
while(!ss.empty() && ss.top() <= i)
{
cout << ss.top();
ss.pop();
}
int t = s.length()-;
while(s[t] != i) t--;
while(now <= t)
{
if(s[now] == i) cout << i;
else ss.push(s[now]);
now++;
}
}
while(!ss.empty())
{
cout << ss.top();
ss.pop();
}
cout << endl;
return ;
}
D.map记录每个数及个数,dfs判断点,每次更新判断区间,若符合,则当前点的值对应的value置为0,最后把每个值的value加起来就可以了。代码写麻烦了,不必要建树的。
#include<bits/stdc++.h>
using namespace std; int n,a[],l[],r[],vis[] = {};
map<int,int> mp; struct xx
{
int x;
xx():l(NULL),r(NULL){};
xx *l,*r;
}*root; void insertt(int now,xx *&p)
{
p = new xx;
p->x = a[now];
if(l[now] > ) insertt(l[now],p->l);
if(r[now] > ) insertt(r[now],p->r);
} void dfs(xx *p,int ll,int rr)
{
if(!p) return;
if(ll < p->x && p->x < rr) mp[p->x] = ;
dfs(p->l,ll,min(p->x,rr));
dfs(p->r,max(p->x,ll),rr);
}
int main()
{
ios::sync_with_stdio(false);
cin >> n;
for(int i = ;i <= n;i++)
{
cin >> a[i] >> l[i] >> r[i];
if(l[i] > ) vis[l[i]] = ;
if(r[i] > ) vis[r[i]] = ;
mp[a[i]]++;
}
root = NULL;
for(int i = ;i <= n;i++)
{
if(vis[i]) continue;
insertt(i,root);
}
dfs(root,-2e9,2e9);
int ans = ;
for(map<int,int>::iterator it = mp.begin();it != mp.end();it++) ans += it->second;
cout << ans << endl;
return ;
}
E.将k分类,k>sqrt(n)时,直接暴力,O(sqrt(n))。其余的k,dp保存答案。
#include<bits/stdc++.h>
using namespace std; int n,q,a[],dp[][]; int main()
{
ios::sync_with_stdio(false);
cin >> n;
for(int i = ;i <= n;i++) cin >> a[i];
for(int j = ;j <= ;j++)
{
for(int i = n;i >= ;i--)
{
if(i+a[i]+j > n) dp[i][j] = ;
else dp[i][j] = dp[i+a[i]+j][j]+;
}
}
cin >> q;
while(q--)
{
int p,k;
cin >> p >> k;
if(k <= ) cout << dp[p][k] << endl;
else
{
int cnt = ;
while(p <= n) p = p+a[p]+k,cnt++;
cout << cnt << endl;
}
}
return ;
}
Codeforces_797的更多相关文章
随机推荐
- 微信小程序之双重循环(包含左滑删除,以及数据各项处理)
<view wx:for="{{hommer}}" wx:for-item="item" wx:for-index="index" w ...
- 海思dv300cv500交叉编译webrtc
感谢声网提供的webrtc国内源码镜像. 首先要安装好海思编译工具链和git. 先替换一下webrtc代码的仓库网址路径 git config --global user.email "10 ...
- JVM之GC算法的实现(垃圾回收器)
上一节:<JVM之GC算法> 知道GC算法的理论基础,我们来看看具体的实现.只有落地的理论,才是真理. 一.JVM垃圾回收器的结构 JVM虚拟机规范对垃圾收集器应该如何实现没有规定,因为没 ...
- (数据科学学习手札72)用pdpipe搭建pandas数据分析流水线
1 简介 在数据分析任务中,从原始数据读入,到最后分析结果出炉,中间绝大部分时间都是在对数据进行一步又一步的加工规整,以流水线(pipeline)的方式完成此过程更有利于梳理分析脉络,也更有利于查错改 ...
- JUnit 5和Selenium基础(二)
使用Selenium内置的PageFactory实现页面对象模式 在这一部分中,将通过Selenium的内置PageFactory支持类来介绍Page Object模式的实现.PageFactory提 ...
- PGSQL 日期时间的比较
pgsql支持日期时间的比较,但是需要注意的是,我们写sql的时候传入的参数一般是字符串类型,我们需要把把字符串转化为Date类型,否则会查不到内容. 例子: select * from user w ...
- 通过例子进阶学习C++(六)你真的能写出约瑟夫环么
本文是通过例子学习C++的第六篇,通过这个例子可以快速入门c++相关的语法. 1.问题描述 n 个人围坐在一个圆桌周围,现在从第 s 个人开始报数,数到第 m 个人,让他出局:然后从出局的下一个人重新 ...
- js css html加载顺序
1.js放在head中会立即执行,阻塞后续的资源下载与执行.因为js有可能会修改dom,如果不阻塞后续的资源下载,dom的操作顺序不可控.正常的网页加载流程是这样的.浏览器一边下载HTML网页,一边开 ...
- 如何选择kmeans中的k值——肘部法则–Elbow Method和轮廓系数–Silhouette Coefficient
肘部法则–Elbow Method 我们知道k-means是以最小化样本与质点平方误差作为目标函数,将每个簇的质点与簇内样本点的平方距离误差和称为畸变程度(distortions),那么,对于一个簇, ...
- numpy 其它常用方法
一.创建特殊的数组 1.ones() 语法 np.ones(shape, dtype=None) # shape 创建数组的shape # dtype 指定数组的数据类型 例子 import nump ...