Codeforces 401C Team(贪心+暴力)
2014-03-12 21:19
531 查看
题目链接:Codeforces 401C Team
题目大意:有n个0和m个1,要求将这些0和1排成一列,保证不会有两个0相邻,不会有连续的三个1,输出方案,不能满足的话输出-1.
解题思路:肯定能确定的是,1最少要分成n-1份,最多分成n+1份,三种情况统统考虑一下即可,注意细节。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n, m;
inline bool judge (int k) {
if (k == 0) return false;
int x = m/k;
return x == 1 || (x == 2 && m%k == 0);
}
void put(int x, int y, int k) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < x; j++) printf("1");
if (i < y) printf("1");
printf("0");
}
}
void solve (int k) {
int x = m/k, y = m%k;
if (k < n) {
printf("0");
put(x, y, k);
} else if (k == n) {
put(x, y, k);
} else {
put(x, y, k-1);
for (int i = 0; i < x; i++) printf("1");
}
printf("\n");
}
int main () {
bool flag = true;
scanf("%d%d", &n, &m);
for (int i = n-1; i <= n+1; i++) if (judge(i)) {
solve (i);
flag = false;
break;
}
if (flag) printf("-1\n");
return 0;
}
题目大意:有n个0和m个1,要求将这些0和1排成一列,保证不会有两个0相邻,不会有连续的三个1,输出方案,不能满足的话输出-1.
解题思路:肯定能确定的是,1最少要分成n-1份,最多分成n+1份,三种情况统统考虑一下即可,注意细节。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n, m;
inline bool judge (int k) {
if (k == 0) return false;
int x = m/k;
return x == 1 || (x == 2 && m%k == 0);
}
void put(int x, int y, int k) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < x; j++) printf("1");
if (i < y) printf("1");
printf("0");
}
}
void solve (int k) {
int x = m/k, y = m%k;
if (k < n) {
printf("0");
put(x, y, k);
} else if (k == n) {
put(x, y, k);
} else {
put(x, y, k-1);
for (int i = 0; i < x; i++) printf("1");
}
printf("\n");
}
int main () {
bool flag = true;
scanf("%d%d", &n, &m);
for (int i = n-1; i <= n+1; i++) if (judge(i)) {
solve (i);
flag = false;
break;
}
if (flag) printf("-1\n");
return 0;
}
相关文章推荐
- CodeForces 723C Polycarp at the Radio (贪心+暴力)
- Codeforces 579D "Or" Game【贪心+暴力按位处理】
- Removing Columns - CodeForces 496 C 【暴力+贪心】
- CodeForces 398A Cards 贪心 暴力 瞎搞
- Codeforces 3 B. Lorry 暴力 排序 枚举 贪心
- CodeForces 707B Bakery (水题,暴力,贪心)
- 【CodeForces - 839B】Game of the Rows(贪心+暴力)(有点坑)
- codeforces 252B Unsorting Array 暴力+贪心
- CodeForces 712C Memory and De-Evolution (贪心+暴力)
- Codeforces-401C-Team
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
- Codeforces 401C Team 贪心法
- Codeforces 891A Pride (dp & 数学 & 贪心 & 暴力)
- Codeforces 734C [水][暴力][贪心]
- codeforces 401C - Team
- Codeforces_357A_Group of Students(暴力、贪心)
- Codeforces 3A. Shortest path of the king(贪心:暴力)
- CodeForces 558 C. Amr and Chemistry && 51NOD 1483 化学变换(暴力 + 贪心)
- !codeforces 399C Cards-数论&贪心-(暴力枚举)
- CodeForces 431# div.2 A Odds and Ends 暴力 贪心