学了一学期还是那么菜。

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


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. NB的程序员,亮瞎了你的眼吗?

    郑重声明: 本文首发于人工博客 1.导读 你能想象到1K的代码能写出什么样的功能强大.效果炫酷的作品吗?来吧,今天小编带领大家认识下下面这位大神的作品. 西班牙程序员Roman Cortes用纯Jav ...

  2. EF 学习系列二 数据库表的创建和表关系配置(Fluent API、Data Annotations、约定)

    上一篇写了<Entity Farmework领域建模方式 3种编程方式>,现在就Code First 继续学习 1.数据库表的创建 新建一个MVC的项目,在引用右击管理NuGet程序包,点 ...

  3. Python 愤怒的小鸟代码实现:物理引擎pymunk使用

    游戏介绍 最近比较忙,周末正好有时间写了python版本的愤怒的小鸟,使用了物理引擎pymunk,图片资源是从github上下载的,实现了一个可玩的简单版本. 功能实现如下: 支持小鸟类型:红色小鸟, ...

  4. 用ES5实现ES6的数组方法map

    先举个常见的栗子: var arr = [1,2,3,4,6,7,8,9,12,3,25,63,100] var arr2 = arr.map(item => item += 1) consol ...

  5. Java程序员必备基础:内部类解析

    前言 整理了一下内部类的相关知识,算是比较全,比较基础的,希望大家一起学习进步. 一.什么是内部类? 在Java中,可以将一个类的定义放在另外一个类的定义内部,这就是内部类.内部类本身就是类的一个属性 ...

  6. MQ队列及常见操作

    一. 创建MQ队列管理器 1.1准备工作 到所安装websphere mq的机子上,进入/opt/mm/bin目录下,查询相关mq的情况,通过命令行./dspmq. 创建mq队列管理器的的时候要用mq ...

  7. http GET 和 POST 请求的优缺点和误区 --前端优化

    Get和Post在面试中一般都会问到,一般的区别:(1)post更安全(不会作为url的一部分,不会被缓存.保存在服务器日志.以及浏览器浏览记录中)(2)post发送的数据更大(get有url长度限制 ...

  8. 【python系统学习06】一张图看懂列表并学会操作

    点击跳转-原文地址 数据类型 - 列表(list) 「目录:」 一张图了解列表 列表是什么 列表长啥样 语法格式 代码示例 格式特征 列表定义 列表操作 - 提取单个:偏移量 什么是偏移量 偏移量提取 ...

  9. python 打印乘法表

    for i in range(1, 10): for j in range(1, i+1): print('%s * %s = %s' % (i, j, i*j), end=' ') print('' ...

  10. MTV

    M:模型 models.py T:模板 html C:控制 urls.py 和 views.py 与MVC类似