欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

3.25 阿里笔试

程序员文章站 2022-03-18 19:15:27
...

题目1:

3.25 阿里笔试

输入:

3.25 阿里笔试

输出:

3.25 阿里笔试

我的思路:只想到了暴力法

typedef long long LL;
int numArray[3][100000];
int main(){
	int n;
	cin >> n;
	vector<LL> sumVec(3,0);
	for(int i = 0; i < 3; ++i){
		for(int j = 0; j < n; ++j){
			cin >> numArray[i][j];
		}
	}
	for(int i = 1; i < n; ++i){
		vector<LL> lastSumVec = sumVec;
		for(int j = 0; j < 3; ++j){
			LL tmpNum = numArray[j][i];
			LL tmpNum1 = abs(tmpNum-numArray[0][i-1]) + lastSumVec[0];
			tmpNum1 = min(tmpNum1,abs(tmpNum-numArray[1][i-1]) + lastSumVec[1]);
			tmpNum1 = min(tmpNum1,abs(tmpNum-numArray[2][i-1]) + lastSumVec[2]);
			sumVec[j] = tmpNum1;
		}
	}
	LL tmpNum = 1e15;
	for(auto it : sumVec)
		tmpNum = min(tmpNum, it);
	cout << tmpNum << endl;
	return 0;
}

 

题目2:

3.25 阿里笔试

输入:

3.25 阿里笔试

输出:

3.25 阿里笔试

我的思路:把每行的第一个整数及差值存储起来

int main(){
	int n,m,q;
	cin >> n >> m >> q;
	vector<vector<int>> numMatrix(n+1);
	for(int i = 0; i <= n; ++ i)
		numMatrix[i].resize(2,0);
	int tmpNum;
	for(int i = 1; i <= n; ++i){
		int firstNum = 0, secondNum = 0, firstIndex ,diff = 0;
		for(int j = 1; j <= m; ++j){
			cin >> tmpNum;
			if(tmpNum > 0 && firstNum == 0){
				firstNum = tmpNum; 
				firstIndex = j;
			}
			else if(tmpNum > 0 && secondNum == 0){
				secondNum = tmpNum;
				diff = (secondNum - firstNum)/(j-firstIndex);
				numMatrix[i][0] = firstNum - (firstIndex-1)*diff; 
				numMatrix[i][1] = diff;
			}
		}
	}
	int tmpX, tmpY;
	while(q--){
		cin >> tmpX >> tmpY;
		if(numMatrix[tmpX][0] == 0)
			cout << "Unknown" << endl;
		else
			cout << numMatrix[tmpX][0] + (tmpY-1)*numMatrix[tmpX][1] <<endl;
	}
	return 0;
}

 

相关标签: 杂记