谷歌中国算法比赛解题报告 APAC2016D

Problem A. Dynamic Grid

Problem B. gBalloon

Problem C. IP Address Summarization

Problem D. Virtual Rabbit


这次比赛最后一题的通过率把我吓尿了,所以打算放到最后做,结果到2017最后一场比赛的前一天我都没空好好研究他们……好吧这就是我推托的理由……


先说说两道做了的题


1.这个题很简单,每次要查询的时候就DFS一下,对每个格子做标记,DFS结束继续遍历矩阵,看能DFS几次就好了


2. 这个题乍一看似乎是动态规划,实际上由于最快回收时间等于需要回收时间最长的那个气球,只要有能量,就尝试更新最慢的那只气球就好了。


3,4 我都没来得及做,如果我能过APAC2017E 就把它们补上……


最后,附上代码

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <hash_map>
#include <hash_set>
#include <unordered_map>
#include <unordered_set>
#include <string.h>
#include <queue>
#include <list>
#include <iomanip>

using namespace std;

#define ll long long
#define uint unsigned int

class PA
{
public:
	PA(){}
	int N, R, C;
	struct Operate
	{
		char type;
		int x, y, z;
		Operate(){ type = 'Q'; x = y = z = 0; }
	};
	vector<Operate> ops;
	char matrix[101][101],tempMatrix[101][101];

	int DFS(int row, int col)
	{
		if (tempMatrix[row][col] == 1) return 0;
		if (matrix[row][col] == '0') return 0;

		tempMatrix[row][col] = 1;
		if (row > 0)
		{
			DFS(row - 1, col);
		}
		if (row < R - 1)
		{
			DFS(row + 1, col);
		}
		if (col>0)
		{
			DFS(row, col - 1);
		}
		if (col < C - 1)
		{
			DFS(row, col + 1);
		}
		return 1;
	}

	void SingleProcess(ofstream& fout)
	{
		for (int i = 0; i < ops.size(); i++)
		{
			if (ops[i].type == 'Q')
			{
				int count = 0;
				memset(tempMatrix, 0, sizeof(tempMatrix));

				for (int row = 0; row < R; row++)
				{
					for (int col = 0; col < C; col++)
					{
						count += DFS(row, col);
					}
				}
				fout << endl << count;
			}
			else
			{
				matrix[ops[i].x][ops[i].y] = '0'+ops[i].z;
			}
		}
	}

	void run()
	{
		FILE* fp = freopen("in.txt", "r", stdin);
		ofstream fout("out.txt");
		int Cases = 0;
		scanf("%d", &Cases);
		for (int time = 0; time < Cases; time++)
		{
			cin >> R >> C;
			memset(matrix, 0, sizeof(matrix));
			for (int i = 0; i < R; i++)
			{
				for (int j = 0; j < C; j++)
				{
					cin >> matrix[i][j];
				}
			}
			cin >> N;
			ops.resize(N);
			for (int i = 0; i < N; i++)
			{
				char ch;
				cin >> ch;
				if (ch == 'Q') ops[i].type = ch;
				else
				{
					ops[i].type = ch;
					cin >> ops[i].x >> ops[i].y >> ops[i].z;
				}
			}


			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};

class PB
{
public:
	PB(){}
	int N, M, Q;
	vector<int> winds;

	struct balloon
	{
		int originPos;
		int currPos;
		int id;
		int posX;
		balloon(){ originPos = currPos = id = posX = 0; }
		int getCollectTime(int speed)
		{
			if ((speed > 0 && posX > 0) || (speed < 0 && posX < 0)) return INT_MAX;
			if (speed == 0) return INT_MAX;
			return abs(posX) / abs(speed) + (abs(posX) % abs(speed) == 0 ? 0 : 1);
		}
	};

	vector<balloon> balloons;

	void SingleProcess(ofstream& fout)
	{
		int minTime = INT_MAX;
		map<int, vector<int>> timeMap;
		map<int, vector<int>>::iterator iter;

		for (int i = 0; i < balloons.size(); i++)
		{
			int time = balloons[i].getCollectTime(winds[balloons[i].currPos]);
			timeMap[time].push_back(i);
		}

		int leftQ = Q;
		while (leftQ>0)
		{
			iter = timeMap.end();
			iter--;
			balloon* curr = &balloons[iter->second.back()];
			iter->second.pop_back();
			minTime = iter->first;
			if (iter->second.empty()) timeMap.erase(iter);

			for (int i = abs(curr->currPos - curr->originPos) + 1;; i++)
			{
				bool valid = false;
				int mint = minTime;
				int pos;
				if (i + curr->originPos< M)
				{
					valid = true;
					int t = curr->getCollectTime(winds[curr->originPos + i]);
					if (t < mint)
					{
						mint = t;
						pos = i + curr->originPos;
					}
				}
				if (curr->originPos - i >= 0)
				{
					valid = true;
					int t = curr->getCollectTime(winds[curr->originPos - i]);
					if (t < mint)
					{
						mint = t;
						pos = curr->originPos - i;
					}
				}
				if (!valid)
				{
					if (minTime == INT_MAX) fout << "IMPOSSIBLE";
					else fout << minTime;
					return;
				}

				if (mint < minTime)
				{
					leftQ -= abs(pos - curr->originPos) - abs(curr->currPos - curr->originPos);
					if (leftQ < 0)
					{
						if (minTime == INT_MAX) fout << "IMPOSSIBLE";
						else fout << minTime;
						return;
					}
					curr->currPos = pos;
					timeMap[mint].push_back(curr->id);
					break;
				}
			}

		}

		if (timeMap.rbegin()->first == INT_MAX) fout << "IMPOSSIBLE";
		else fout << timeMap.rbegin()->first;

	}

	void run()
	{
		FILE* fp = freopen("in.txt", "r", stdin);
		ofstream fout("out.txt");
		int Cases = 0;
		scanf("%d", &Cases);
		for (int time = 0; time < Cases; time++)
		{
			scanf("%d %d %d", &N, &M, &Q);
			winds.resize(M, 0);
			for (int i = 0; i < M; i++)
			{
				scanf("%d", &winds[i]);
			}
			balloons.resize(N, balloon());
			for (int i = 0; i < N; i++)
			{
				scanf("%d %d", &balloons[i].posX,&balloons[i].originPos);
				balloons[i].id = i;
				balloons[i].currPos = balloons[i].originPos;
			}

			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};

class PC
{
public:
	PC(){}
	int M, N;
	struct NormalizeIp
	{
		uint lowerBound;
		uint upperBound;
		uint mask;
		NormalizeIp(){ lowerBound = upperBound = 0;}
		void initIpBould(string ip)
		{
			convertIPtoInt(ip, lowerBound, mask);
			upperBound = lowerBound + (0x7000000 >> (mask-1));
		}

		void convertIPtoInt(string ip, uint& int_ip, uint& mask)
		{
			unsigned int ret, temp;
			string::size_type st = ip.find_first_of('.');
			string val = ip.substr(0, st);
			ip = ip.substr(st + 1);
			temp = atoi(val.c_str());
			ret |= temp << 24;

			st = ip.find_first_of('.');
			val = ip.substr(0, st);
			ip = ip.substr(st + 1);
			temp = atoi(val.c_str());
			ret |= temp << 16;

			st = ip.find_first_of('.');
			val = ip.substr(0, st);
			ip = ip.substr(st + 1);
			temp = atoi(val.c_str());
			ret |= temp << 8;

			st = ip.find_first_of('/');
			val = ip.substr(0, st);
			ip = ip.substr(st + 1);
			temp = atoi(val.c_str());
			ret |= temp;

			temp = atoi(ip.c_str());
			ret = ret >> (32 - temp);
			ret = ret << (32 - temp);
			int_ip = ret;
			mask = temp;
		}

		string convertIntToIp(uint int_ip)
		{
			string ret = "";
			int count = 0;
			int temp = 1;
			while ((temp&int_ip) == 0)
			{
				count++;
				temp = temp << 1;
			}
			temp = int_ip >> 24;
			char ch[16];
			_itoa(temp, ch, 10);
			ret += ch;

			temp = int_ip << 8;
			temp = temp >> 24;
			_itoa(temp, ch, 10);
			ret += ".";
			ret += ch;

			temp = int_ip << 16;
			temp = temp >> 24;
			_itoa(temp, ch, 10);
			ret += ".";
			ret += ch;

			temp = int_ip << 24;
			temp = temp >> 24;
			_itoa(temp, ch, 10);
			ret += ".";
			ret += ch;

			ret += "/";
			_itoa(count, ch, 10);
			ret += ch;

			return ret;
		}
	};

	vector<set<pair<int, int>>> prefixSet;
	vector<NormalizeIp> ipvec;

	

	

	void SingleProcess(ofstream& fout)
	{
		

	}

	void run()
	{
		FILE* fp = freopen("in.txt", "r", stdin);
		ofstream fout("out.txt");
		int Cases = 0;
		scanf("%d", &Cases);
		for (int time = 0; time < Cases; time++)
		{
			scanf("%d", &N);
			ipvec.resize(N);
			char ch[1024];
			for (int i = 0; i < N; i++)
			{
				scanf("%s", ch);
				ipvec[i].initIpBould(ch);
			}

			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};

class PD
{
public:
	PD(){}
	int M, N;

	void SingleProcess(ofstream& fout)
	{
		
	}

	void run()
	{
		FILE* fp = freopen("in.txt", "r", stdin);
		ofstream fout("out.txt");
		int Cases = 0;
		scanf("%d", &Cases);
		for (int time = 0; time < Cases; time++)
		{
			scanf("%d %d", &M, &N);


			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};





int main()
{
	//PA p;
	//PB p;
	PC p;
	//PD p;
	p.run();

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值