学了一学期还是那么菜。

好久没更新,还是得放点东西的。


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的更多相关文章

随机推荐

  1. wrk性能测试(详解)

    一.简介 wrk 是一款针对 Http 协议的基准测试工具,它能够在单机多核 CPU 的条件下,使用系统自带的高性能 I/O 机制,如 epoll,kqueue 等,通过多线程和事件模式,对目标机器产 ...

  2. 用python做推荐系统(二)

    一.简介 继上一篇基于用户的推荐算法,这一篇是要基于商品的,基于用户的好处是可以根据用户的评价记录找出跟他兴趣相似的用户,再推荐这些用户也喜欢的电影,但是万一这个用户是新用户呢?或是他还没有对任何电影 ...

  3. 学以致用,react学习前奏准备阶段

    ReactJS:支持React开发,提供JSX代码提示,高亮显示,ReactJS官方介绍 1.cdm→  componentDidMount: fn() { ... }   cdm 2.cdup→  ...

  4. JUnit 5和Selenium基础(一)

    Gradle.JUnit 5和Jupiter Selenium Selenium是一组支持浏览器自动化的工具,主要用于Web应用程序测试.Selenium的组件之一是Selenium WebDrive ...

  5. ArcGIS Desktop 10.1 下载地址及破解

    ArcGIS Desktop 10.1 正式版请到这里下载 http://pan.baidu.com/share/link?shareid=27476&uk=3608003693 正式版破解方 ...

  6. echarts圆饼图设置默认选中项并在中间显示文字

    效果: 代码: var myChart = echarts.init(document.getElementById('quanshi-echarts-two')); option = { grid: ...

  7. windows创建git并连结github

    1.下载跟自己系统相对应的git版本 2.默认安装 3.绑定用户 git config --global user.name ""git config --global user. ...

  8. 带限制的广搜 codeforces

    You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each ...

  9. mysql 查询中文数据

    select * from comm_user WHERE length(tags)!=CHAR_LENGTH(tags)

  10. scrapy selector选择器

    这部分内容属于补充内容 1.xpath() 2.css() 3.正则表达式 # 多个值,列表 response.xpath('//a/text()').re('(.*?):\s(.*)') # 取第一 ...