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的更多相关文章
随机推荐
- 实现antd下拉框动态添加内容(与数据库交互)
antd下拉控件的动态内容添加(与数据库交互) antd这个框架给开发带来了极大的方便,但同时,我认为还有一些不方便的地方:常用的逻辑在文档中没有体现.需要前端开发经验的人才能快速上手,而我刚刚接触这 ...
- 【转】分布式服务框架 Zookeeper -- 管理分布式环境中的数据
Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理 ...
- ArcGIS for JavaScript 开发智能提示
开发如果没有智能提示,可想而知是一件多举痛苦的事情,好在Esri为Visual Studio 2010.Aptana3提供了一个插件,这样就使我们在使用ArcGIS API for Javascrip ...
- Oozie任务调度框架详解及使用简介(一)
摘要:个人最近一段时间一直在使用oozie,从刚开始的各种别扭到现在越来越觉得有意思的情况下,想整理一下关于oozie的认知,整理出来一个oozie系列,本来市面上关于oozie的资料就比较少,希望写 ...
- mongo windows 安装
下载安装包 一路next 打开cmd 或者 power shell 准备本地目录. D: #进入d盘 md data #创建目录 data cd data #进入目录 data md config # ...
- 通过VS2019使用Web部署发布.net core程序
服务器:Windows Server2012R2 服务器已安装好IIS 需要启用Web Management Service 与 Web部署代理服务 服务器默认是没有Web部署代理服务的 需要安装 ...
- 【JavaScript 基础知识】一篇关于 JavaScript 一些知识点的总结
JavaScript 中基础数据类型 数据类型名称 数据类型说明 Undefined 只有一个值,即 undefined ,声明变量的初始值. Null 只有一个值,即 null ,表示空指针, ...
- Linux 断网安装MySQL5.x操作步骤
1.下载MySQL安装包 由于官网下载比较慢,暂时我这边采用的方法是通过网易云的centos镜像下载的地址是 http://mirrors.163.com/mysql/Downloads/MySQL- ...
- (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520
题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...
- 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! Spring Cloud Eureka 基于Netflix Eureka做了二次封装(Spring Clo ...