Submission #400450

# Submission time Handle Problem Language Result Execution time Memory
400450 2021-05-08T05:03:58 Z galca MalnaRISC (COI21_malnarisc) C++14
40 / 100
2 ms 336 KB
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int N;
	cin >> N;

	int n = N;
	while (n & (n - 1)) {
		n++;
	}

   vector<int> regs(n);
   for (int i=0; i<n; i++) {
	  regs[i] = i+1;
   }  

   vector<int> stage;
   vector<int> r1;
   vector<int> r2;

   int curr_stage = 0;

   for (int k = 2; k <= n; k *= 2) {
	   for (int j = k / 2; j > 0; j /= 2) {
		   cout << endl;
		   for (int i = 0; i < n; i++) {
			   int ip = i ^ j;
			   if (ip > i) {
				   int reg1, reg2;
				   if ((i ^ k) < i) {
					   reg1 = regs[i];
					   reg2 = regs[ip];
				   }
				   else {
					   reg1 = regs[ip];
					   reg2 = regs[i];
				   }
				   if ((reg2 <= N) && (reg1 <= N)) {
					   stage.push_back(curr_stage);
					   r1.push_back(reg2);
					   r2.push_back(reg1);
					   //cout << "CMPSWP R" << reg2 << " R" << reg1 << endl;
				   } else {
					   if ((reg2 > N) && (reg1 > N)) {
						   // do nothing
					   }
					   else {
						   if (reg1 > N) {
							   regs[ip] = reg2;
							   regs[i] = reg2;
						   }
						   else {
							   regs[ip] = reg1;
							   regs[i] = reg1;
						   }
						   stage.push_back(curr_stage);
						   r1.push_back(regs[ip]);
						   r2.push_back(regs[i]);
						  // cout << "CMPSWP R" << regs[ip] << " R" << regs[i] << endl;
					   }
				   }
			   }
		   }
		   ++curr_stage;
	   }
   }
   
   cout << curr_stage << endl;
   curr_stage = 0;
   for (int i = 0; i < r1.size(); i++) {
	   if (stage[i] != curr_stage) {
		   cout << endl;
	   }
	   curr_stage = stage[i];
	   cout << "CMPSWP R" << r1[i] << " R" << r2[i] << " ";
   }
   return 0;
}

Compilation message

malnarisc.cpp: In function 'int main()':
malnarisc.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |    for (int i = 0; i < r1.size(); i++) {
      |                    ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 332 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 332 KB Output isn't correct