CodeForces:Cards
来源:互联网 发布:身份证图像识别 java 编辑:程序博客网 时间:2024/06/07 16:11
Description
User ainta loves to play with cards. He has a cards containing letter “o” and b cards containing letter “x”. He arranges the cards in a row, and calculates the score of the deck by the formula below.
At first, the score is 0.
For each block of contiguous “o”s with length x the score increases by x2.
For each block of contiguous “x”s with length y the score decreases by y2.
For example, if a = 6, b = 3 and ainta have arranged the cards in the order, that is described by string “ooxoooxxo”, the score of the deck equals 22 - 12 + 32 - 22 + 12 = 9. That is because the deck has 5 blocks in total: “oo”, “x”, “ooo”, “xx”, “o”.
User ainta likes big numbers, so he wants to maximize the score with the given cards. Help ainta make the score as big as possible. Note, that he has to arrange all his cards.
Input
The first line contains two space-separated integers a and b (0 ≤ a, b ≤ 105; a + b ≥ 1) — the number of “o” cards and the number of “x” cards.
Output
In the first line print a single integer v — the maximum score that ainta can obtain.
In the second line print a + b characters describing the deck. If the k-th card of the deck contains “o”, the k-th character must be “o”. If the k-th card of the deck contains “x”, the k-th character must be “x”. The number of “o” characters must be equal to a, and the number of “x ” characters must be equal to b. If there are many ways to maximize v, print any.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Sample Input
Input
2 3
Output
-1
xoxox
Input
4 0
Output
16
oooo
Input
0 4
Output
-16
xxxx
思路:
贪心思路,应该尽量使得o连续,x分散。
可以证明,尽量使用一个o来分割x最后得到的结果会更大。
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;typedef long long ll;const ll INF = 0x3f3f3f3f3f3f3f3f;ll a, b, ans, v = 1;ll solve (ll k) { ll x = b/k, c = b%k; ll ans = (a-k+2)*(a-k+2) +(k-2); ll tmp = c*(x+1)*(x+1) + (k-c)*x*x; return ans - tmp;}void put() { cout << ans << endl; ll x = b/v, c = b%v; bool flag = false; for (ll i = 1; i < v; i++) { if (flag) printf("o"); for (ll j = 0; j < x; j++) printf("x"); if (i <= c) printf("x"); flag = true; } for (ll i = 0; i < (a-v+2); i++) printf("o"); for (ll i = 0; i < x; i++) printf("x"); printf("\n");}int main () { ans = -INF; cin >> a >> b; if (a == 0) { cout << b*b*(-1) << endl; for (ll i = 0; i < b; i++) printf("x"); printf("\n"); return 0; } for (ll i = 2; i <= a + 1; i++) { //枚举o将x分成的分数 ll t = solve(i); if (t > ans) { ans = t; v = i; } } put(); return 0;}
- CodeForces:Cards
- codeforces#233_div2_C Cards
- Codeforces 398 A. Cards
- CodeForces 398A Cards
- codeforces Vanya and Cards
- CodeForces-626B-Cards
- codeforces--701A Cards
- CodeForces 701A Cards
- codeforces 701 A. Cards
- codeforces 731FVideo Cards
- Cards CodeForces 399C
- codeforces 701A Cards
- codeforces A. Vanya and Cards
- Codeforces 398A Cards 暴力
- CodeForces 399C Cards (数论)
- Codeforces 626B Cards(规律)
- CodeForces 626B-Cards【DFS】
- CodeForces 626B:Cards【模拟】
- 【玖哥乱弹】编程语言间的斗争
- 工厂模式
- URLConnection &HttpURLConnection 网络请求
- APN显示虚拟运营商名称(sprd5.0)
- OS X 执行命令加了sudo还是提示Operation not permitted
- CodeForces:Cards
- C++面试中关于智能指针的问题
- 测试人员如何突破自我的瓶颈?
- [Leetcode] 274. H-Index 解题报告
- BZOJ 3534: [Sdoi2014]重建 矩阵树定理
- 解决adb devices无法识别到手机问题
- php模拟post提交请求,调用接口
- React-Router 学习笔记
- 关于程序员面试