夢追い人

"It takes a dreamer to make a dream come true."―Vincent Willem van Gogh

昨日のやけくそ精進

0118

#include <iostream>
#include <vector>
using namespace std;
typedef vector<string> vs;
int dx[] = {0,0,1,-1};
int dy[] = {-1,1,0,0};
int H, W;
vs dfs(vs map, int ny, int nx, char now) {
	map[ny][nx] = '-';
	for (int i=0; i<4; i++) {
		if (ny+dy[i]>=0&&ny+dy[i]<H&&
		nx+dx[i]>=0&&nx+dx[i]<W&&map[ny+dy[i]][nx+dx[i]]==now) {
			map = dfs(map, ny+dy[i], nx+dx[i], now);
		}
	}
	return map;
}
int main() {
	while (cin>>H>>W) {
		if (H==0&&W==0) break;
		vs map(H);
		for (int i=0; i<H; i++) cin>>map[i];
		int res = 0;
		for (int i=0; i<H; i++) {
			for (int j=0; j<W; j++) {
				if (map[i][j]!='-') {
					map = dfs(map, i, j, map[i][j]);
					res++;
				}
			}
		}
		cout<<res<<endl;
	}
}

0100

#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main() {
	int n;
	while (scanf("%d",&n)&&n!=0) {
		map<int, long long> sales;
		vector<int> log;
		long long prc, mnt;
		int num;
		for (int i=0; i<n; i++) {
			scanf("%d%lld%lld",&num,&prc,&mnt);
			if (!sales[num]) {
				log.push_back(num);
			}
			sales[num] += prc * mnt;
		}
		bool flag = true;
		for (int i=0; i<log.size(); i++) {
			if (sales[log[i]]>=1000000) {
				flag = false;
				printf("%d\n", log[i]);
			}
		}
		if (flag) printf("NA\n");
	}
}

0112

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
	int n;
	while (scanf("%d",&n)&&n!=0) {
		vector<int> cast(n);
		for (int i=0; i<n; i++) scanf("%d",&cast[i]);
		sort(cast.begin(), cast.end());
		long long res = 0;
		long long mati = 0;
		for (int i=0; i<n; i++) {
			res += mati;
			mati += cast[i];
		}
		printf("%lld\n", res);
	}
}

0108

#include <cstdio>
#include <vector>
#include <map>
using namespace std;
typedef vector<int> vi;

vi C;

bool compare(vi S, vi C) {
	for (int i=0; i<S.size(); i++) {
		if (S[i] != C[i]) return false;
	}
	return true;
}

int solve(vi S, int cnt) {
	C = vi(S.size());
	map<int, int> ma;
	for (int i=0; i<S.size(); i++) {
		if (ma.empty() || ma.find(S[i])==ma.end()) {
			ma[S[i]] = 1;
		} else {
			ma[S[i]]++;
		}
	}
	for (int i=0; i<S.size(); i++) {
		C[i] = ma[S[i]];
	}
	if (compare(S, C)) {
		return cnt;
	}
	return solve(C, cnt+1);
}

int main() {
	int n;
	while (scanf("%d",&n)&&n!=0) {
		vi S(n);
		for (int i=0; i<n; i++) scanf("%d",&S[i]);
		int res = solve(S, 0);
		printf("%d\n", res);
		for (int i=0; i<C.size()-1; i++) printf("%d ",C[i]);
		printf("%d\n", C[C.size()-1]);
	}
}

アルゴリズム〜〜〜〜〜〜

広告を非表示にする