Codeforces 628C Bear and String Distance 【构造】
来源:互联网 发布:淘宝宝贝复制破解 编辑:程序博客网 时间:2024/06/08 11:21
题目链接:点我
题意:定义dist(字母1, 字母2) = 两字母ascll之差。
给定一个串a和一个值k,找到另一个串b(长度相等)使得两串各个位置的dist()之和 == k。
思路:每次尽可能大的去补,补得范围在min(k, 当前字母可以补的最大范围)。最后判断k 是否为 0。
sort一下并没有什么用。。。
AC代码:
#include <iostream>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <cstdlib>#define CLR(a, b) memset(a, (b), sizeof(a))#define PI acos(-1.0)using namespace std;typedef long long LL;typedef double DD;const int MAXN = 1e5+10;struct Node{ int val, id;};Node num[MAXN];bool cmp1(Node a, Node b){ return a.val > b.val;}int cnt[MAXN];int main(){ int n, k; cin >> n >> k; string str, ans; cin >> str; for(int i = 0; i < n; i++) { num[i].val = str[i] - 'a' + 1; num[i].id = i; } sort(num, num+n, cmp1); for(int i = 0; i < n; i++) { int Max = max(26 - num[i].val, num[i].val - 1); int temp = min(Max, k); if(num[i].val + temp <= 26) cnt[num[i].id] = num[i].val + temp; else cnt[num[i].id] = num[i].val - temp; k -= temp; } if(k) cout << -1 << endl; else { ans = ""; for(int i = 0; i < n; i++) ans += 'a' + (cnt[i]-1); cout << ans << endl; } return 0;}
0 0
- Codeforces 628C Bear and String Distance 【构造】
- codeforces 628C Bear and String Distance
- CodeForces 628C - Bear and String Distance
- CodeForces 628C Bear and String Distance
- CodeForces 628 C C - Bear and String Distance
- Codeforces C - Bear and String Distance (贪心)
- CodeForces 628 C. Bear and String Distance(贪心)
- CodeForces 628C Bear and String Distance (水题)
- 【CodeForces 628C 】Bear and String Distance (贪心)
- CodeForces - 628C Bear and String Distance (模拟)
- Codeforces 628 C. Bear and String Distance【贪心】
- codeforces 628C Bear and String Distance 水
- coeforces 628C. Bear and String Distance
- Educational Codeforces Round 8 C. Bear and String Distance(贪心)
- codeforces_628C. Bear and String Distance
- Codeforces 658C Bear and Forgotten Tree 3【构造】
- CodeForces 658C Bear and Forgotten Tree 3(构造)
- Codeforces 791C Bear and Different Names 简单构造
- Android的通讯员——notification
- spark源码学习(六)--- DAGScheduler中的task的划分
- oracle之函数使用大全
- android小问题--------------------SQLiteDatabase.insert(table, nullColumnHack, values)参数
- 测试必备技能系列4:如何用SSH向linux服务器上传下载文件
- Codeforces 628C Bear and String Distance 【构造】
- java中transient关键字
- test
- 多线程
- [BZOJ3626] [LNOI2014]LCA
- OS X EL Capitan 开启适用于Force Touch的三指拖拽
- ArrayList源码分析
- mysql处理高并发,防止库存超卖
- android 5.1预置apk