CSES - Putka Open 2015 – 6/6 - Results
Submission details
Task:Bittilista
Sender:
Submission time:2015-12-06 18:20:47 +0200
Language:C++
Status:READY
Result:45
Feedback
groupverdictscore
#1ACCEPTED17
#2ACCEPTED28
#30
Test results
testverdicttimegroup
#1ACCEPTED0.06 s1details
#2ACCEPTED0.05 s1details
#3ACCEPTED0.06 s1details
#4ACCEPTED0.06 s1details
#5ACCEPTED0.05 s1details
#6ACCEPTED0.05 s2details
#7ACCEPTED0.06 s2details
#8ACCEPTED0.06 s2details
#9ACCEPTED0.06 s2details
#10ACCEPTED0.05 s2details
#110.25 s3details
#120.25 s3details
#130.26 s3details
#140.27 s3details
#150.27 s3details

Compiler report

input/code.cpp: In function 'bool isEven(std::vector<bool>&)':
input/code.cpp:45:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < b.size(); i++) {
                    ^
input/code.cpp: In function 'int getDiff(std::vector<bool>&)':
input/code.cpp:56:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < b.size(); i++) {
                    ^

Code

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <set>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <tuple>


using namespace std;
typedef long long LL;


vector<vector<bool>> create(int bits) {
	LL total = (LL)1<<bits;
	vector<vector<bool>> res(total, vector<bool>(bits));
	bool cur = true;
	LL switching = total/2;
	for (int col = 0; col < bits; col++) {
		for (int i = 0; i < total; i++) {
			if (i % switching == 0) cur = !cur;
			res[i][col] = cur;
		}
		switching /= 2;
	}
	return res;
}


bool isEven(vector<bool> & b) {
	if (b.size() == 1) return true;
	int zeroone = 0;
	int onezero = 0;
	for (int i = 1; i < b.size(); i++) {
		if (b[i-1] == 0 && b[i] == 1) zeroone++;
		else if (b[i-1] == 1 && b[i] == 0) onezero++;
	}
	return zeroone == onezero;
}

int getDiff(vector<bool> & b) { // get diff of "01"s minus "10"s
	if (b.size() == 1) return 0;
	int zeroone = 0;
	int onezero = 0;
	for (int i = 1; i < b.size(); i++) {
		if (b[i-1] == 0 && b[i] == 1) zeroone++;
		else if (b[i-1] == 1 && b[i] == 0) onezero++;
	}
	return zeroone - onezero;
}

void printres(vector<bool> b1, vector<bool> b2) {
	for (bool b : b1) {
		cout << (int) b;
	}
	for (bool b : b2) {
		cout << (int) b;
	}
}

void printvec(vector<bool> b1) {
	for (bool b : b1) {
		cout << (int) b;
	}
	cout << endl;
}


void brute(int n, int k) {
	auto v = create(n);
//cout << "qwewqe" << endl;
	vector<vector<bool>> asd;
	for (auto v1 : v) {
		if (isEven(v1)) {
			asd.push_back(v1);
		}
	}
//cout << "asda" << endl;
	sort(asd.begin(), asd.end());
	printvec(asd[k-1]);
}


int main() {
	int n, k;
	cin >> n >> k;

	if (n <= 10) {
		brute(n,k);
		return 0;
	}
	//
	//

	int bits1 = n/2;
	int bits2 = n/2 + n%2;

	vector<vector<bool>> r1 = create(bits1);
	vector<vector<bool>> r2 = create(bits2);
//for (auto v : r2) printvec(v);
//cout << endl;
	map<int, vector<vector<bool>>> startWithOne;
	map<int, vector<vector<bool>>> startWithZero;

	for (auto & v : r2) {
		int diff = getDiff(v);
		if (v[0] == 0) {
			startWithZero[diff].push_back(v);
		} else {
			startWithOne[diff].push_back(v);
		}
	}

	// TODO sort values
	sort(r1.begin(), r1.end());

	//n--; // TODO: ok?
	for (auto & v : r1) {
		int diff = getDiff(v);

		int s = 0;
		if (v.back() == 0) {
			s += startWithZero[-diff].size();
			s += startWithOne[-diff - 1].size();
		} else {
			s += startWithZero[-diff + 1].size();
			s += startWithOne[-diff].size();
		}

//printvec(v);
//cout << diff << " " << s << endl;

		if (k <= s) {
			vector<vector<bool>> temp;
			if (v.back() == 0) {
				move(startWithZero[-diff].begin(), startWithZero[-diff].end(), back_inserter(temp));
				move(startWithOne[-diff - 1].begin(), startWithOne[-diff - 1].end(), back_inserter(temp));
			} else {
				move(startWithZero[-diff + 1].begin(), startWithZero[-diff + 1].end(), back_inserter(temp));
				move(startWithOne[-diff].begin(), startWithOne[-diff].end(), back_inserter(temp));
			}
			sort(temp.begin(), temp.end());			
			
			printres(v, temp[k-1]);
			return 0;
		} else {
			k -= s;
		}
	}

	/*
	for (auto & v : r1) {
		if (isEven(v)) {
			res1.push_back(v);
		}
	}

	for (auto & v : r2) {
		if (isEven(v)) {
			res2.push_back(v);
		}
	}


	sort(res2.begin(), res2.end());

	n--; // TODO ONE INDEXED correct?
	int first = n/res2.size();
	int second = n % res2.size();
	
	for (auto v : res1)	{ for (bool b : v) { cout << (int) b; } cout << endl; }

cout << res1.size() << " sizes " << res2.size() << endl;
	for (bool b : res1[first]) {
		cout << (int) b;
	}
cout << endl;
	for (bool b : res2[second]) {
		cout << (int) b;
	}
	*/

}













Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10 54

correct output
0001101010

user output
0001101010

Test 2

Group: 1

Verdict: ACCEPTED

input
10 302

correct output
1001011011

user output
1001011011

Test 3

Group: 1

Verdict: ACCEPTED

input
10 241

correct output
0111100000

user output
0111100000

Test 4

Group: 1

Verdict: ACCEPTED

input
10 382

correct output
1011111011

user output
1011111011

Test 5

Group: 1

Verdict: ACCEPTED

input
10 138

correct output
0100010010

user output
0100010010

Test 6

Group: 2

Verdict: ACCEPTED

input
20 131002

correct output
00111111111101110010

user output
00111111111101110010

Test 7

Group: 2

Verdict: ACCEPTED

input
20 441567

correct output
11010111100110111101

user output
11010111100110111101

Test 8

Group: 2

Verdict: ACCEPTED

input
20 109770

correct output
00110101100110010010

user output
00110101100110010010

Test 9

Group: 2

Verdict: ACCEPTED

input
20 327308

correct output
10011111110100010111

user output
10011111110100010111

Test 10

Group: 2

Verdict: ACCEPTED

input
20 302918

correct output
10010011111010001011

user output
10010011111010001011

Test 11

Group: 3

Verdict:

input
50 216967103451763

correct output
011000101010101001001011100100...

user output
(empty)

Test 12

Group: 3

Verdict:

input
50 236618662270629

correct output
011010111001101000001001101001...

user output
(empty)

Test 13

Group: 3

Verdict:

input
50 426560943304480

correct output
110000011111101000111010110000...

user output
(empty)

Test 14

Group: 3

Verdict:

input
50 294553802415801

correct output
100001011111001010010011011000...

user output
(empty)

Test 15

Group: 3

Verdict:

input
50 502225394100883

correct output
111001000110001010111011000110...

user output
(empty)