読者です 読者をやめる 読者になる 読者になる

TopCoder SRM 398 DIV 2 (Practice)

Programming Algorithm TopCoder

SRM 499前にやった。

250: MinDifference

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

class MinDifference {
public:
	int closestElements(int A0, int X, int Y, int M, int n) {
		vector<int> A(n);
		A[0] = A0;
		for(int i = 1; i < n; i++){
			A[i] = (A[i - 1] * X + Y) % M;
		}
		sort(A.begin(), A.end());
		int minimum = INT_MAX;
		for(int i = 0; i < n - 1; i++){
			minimum = min(minimum, A[i + 1] - A[i]);
		}
		return minimum;
	}

	

};

500: CountExpressions

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

class CountExpressions {
public:
	int calcExpressions(int x, int y, int val) {
		vector<int> v;
		v.push_back(x); v.push_back(x);
		v.push_back(y); v.push_back(y);
		sort(v.begin(), v.end());
		int cnt = 0;
		do {
			queue<pair<int, int> > q;
			q.push(make_pair(v[0], 0));
			while(!q.empty()){
				pair<int, int> t = q.front(); q.pop();
				if(t.second >= 3){
					if(t.first == val) cnt++;
					continue;
				}
				int s;
				s = t.first + v[t.second + 1]; q.push(make_pair(s, t.second + 1));
				/*if(*/(s = t.first - v[t.second + 1]);/*>= 0)*/ q.push(make_pair(s, t.second + 1));
				/*if(*/(s = t.first * v[t.second + 1]);/* >= 0)*/ q.push(make_pair(s, t.second + 1));
			}
		} while(next_permutation(v.begin(), v.end()));
		return cnt;
	}

	

};

900:

実はよく分かっていない