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的更多相关文章
随机推荐
- 小小知识点(十八)U盘中病毒了,System Volume Information文件夹删除不掉
win+R调出命令窗口后搜索cmd,启用cmd命令编辑器,并输入以下命令: attrib "H:\System Volume Information" -s //这句话可以选择 ...
- 《C++Primer》第五版习题答案--第一章【学习笔记】
C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...
- css label两端对齐
上面这种效果很常见,实现的代码如下: html部分 <ul> <li class="detail_item"> <span class="d ...
- STM32串口遇到的一个问题
做HLW8032电能表项目中关于USART使用DMA接收定长数据的问题 1:由于HLW8032芯片一上电,芯片就会通过串口每隔50ms向STM32发送24字节的数据,且我不能通过STM32控制HLW8 ...
- 关于django中的get_or_create方法的坑
最近在项目中发现了这样的一个坑,那就是我们的需求是不能添加一个相同的对象到数据库中,就通过某些字段的值组合成唯一值到数据库中去查找数据,如果没有找到对象,那就创建一条新的数据库记录,而刚好django ...
- String.valueOf(null)
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); ...
- Android布局属性与常用控件
一.Android常用布局属性 1. LinearLayout的特有属性 android:orientation:设置布局排列方式 android:layout_weight:设置所占布局的权重 ...
- Excel-条件格式
今天运用了一下条件格式中的自建规则进行公式筛选, 设置格式那里一定要将$P$8修改为$P8 然后双击修改后的第一项进行单元格的自动填充
- ORM基础4 跨表查询+原子性操作
一.跨表查询 1.# # 正向查找 对象查找 # book_obj = models.Book.objects.get(id=3) # print(book_obj) # ret = book_obj ...
- [bzoj3938] [Uoj #88] Robot
Description 小 \(q\) 有 \(n\) 只机器人,一开始他把机器人放在了一条数轴上,第 \(i\) 只机器人在 \(a_i\) 的位置上静止,而自己站在原点.在这之后小 \(q\) 会 ...