Codeforces 849 C. From Y to Y 结论题
来源:互联网 发布:酒店淡季网络促销活动 编辑:程序博客网 时间:2024/06/05 09:49
题意
给出一个数k(0-100000) , 构造一个非空字符串s,使得字符串中各字母的合成和恰好为k.
枚举发现,每个字母的合成和都是独立的,且恰好等于n(n-1)/2,其中n是这个字母的出现次数.
解法
题目转化为把k表示成26个以内的给定数的和.
最开始想着不会多重背包的二进制优化,完了.
但是题目中说解法必定存在,所以只要从大到小取能取的数即可.
一个坑点是,题目要求非空字符串,所以需要特判k=0的情况.
代码
#include <bits/stdc++.h>using namespace std;int save[1000], num[1000];int main(void){ for(int i = 1; i < 1000; i++) { save[i] = i * (i - 1) / 2; } int k; scanf("%d", &k); if(k == 0) { printf("a\n"); } else { while(k) { int x = upper_bound(save, save + 1000, k) - save - 1; k -= save[x]; num[x]++; } char ch = 'a'; for(int i = 0; i < 1000; i++) { while(num[i]) { for(int j = 0; j < i; j++) { printf("%c", ch ); } num[i]--; ch++; } } printf("\n"); } return 0;}
总结
看清楚题,多想想边界条件.
阅读全文
0 0
- Codeforces 849 C. From Y to Y 结论题
- Codeforces 849 C From Y to Y
- Codeforces 849 C. From Y to Y (技巧)
- Codeforces 849C From Y to Y【思维】
- Codeforces From Y to Y
- CodeForces #431 Div. 2 849C From Y to Y 贪心 技巧题
- C. From Y to Y
- Codeforces Round #431 (Div. 2) C. From Y to Y(补题)
- Codeforces Round #431 (Div. 2) From Y to Y
- codeforces 848A From Y to Y(思维)
- Codeforces848A-From Y to Y
- Codeforces849C From Y to Y
- Codeforces Round #431 (Div. 2) A、Odds and Ends B、Tell Your World C、From Y to Y
- CF848A From Y to Y【思路】
- codeforce 848A From Y to Y
- Codeforces Round #431 (Div. 2) 848A From Y to Y(思维)
- CF 848 A. From Y to Y【模拟】
- Convert from type X to type Y
- WOJ1124-Football Coach
- 3d3d
- 新手如何学习Java——Java学习路线图
- LeetCode刷题指南
- python
- Codeforces 849 C. From Y to Y 结论题
- 集合各实现类的底层实现原理
- UVA 1593
- 92. Reverse Linked List II(链表)
- SSM框架项目开发过程整理
- List集合
- 交叉编译环境以及ARM开发板上-/bin/sh: ./hello: not found
- 《Deep Work -- 深度工作》
- CodeForces 281 A.Word Capitalization(水~)