社区讨论

本地奇怪输出

学术版参与者 1已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mi7yzk9u
此快照首次捕获于
2025/11/21 05:55
4 个月前
此快照最后确认于
2025/11/21 05:55
4 个月前
查看原帖
本地比在线IDE多输出4行??
CPP
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int INF = 1e9;
struct City
{
	int num , h;
	bool operator < (const City &a) const
	{
		return h < a.h;
	}
}city[100010];
int n , m , dis1 , dis2 , pit[100010] , from[100010] , ord[100010] , go[100010][3];
int pas1[22][100010] , pas2[22][100010] , cost[22][100010] , f[22][100010];

void baseone()	
{
	for(int i = 1; i <= n; i++)
	{
		ord[city[i].num] = i;
		from[i] = i - 1 , pit[i] = i + 1;
	}
	for(int i = 1; i <= n; i++)
	{
		int now = ord[i] , frm = from[now] , nxt = pit[now];
		for(int j = 1; j <= 2; j++)
		{
			if(!frm && nxt > n) continue;
			if(!frm || nxt > n)
			{
				if(!frm)
					go[now][j] = nxt , nxt = pit[nxt];
			    else
					go[now][j] = frm , frm = from[frm];
				continue;
			}
			if(city[nxt].h - city[now].h < city[now].h - city[frm].h)
				go[now][j] = nxt , nxt = pit[nxt];
			else 
				go[now][j] = frm , frm = from[frm];
		}
		pit[from[now]] = pit[now] , from[pit[now]] = from[now];
	}
}

void checkone()
{
	cout << "checkone now" << endl;
	for(int i = 1; i <= n; i++)
	{
		int now = ord[i];
		cout << "num "<< now << ' ' << city[now].h << endl;
		cout << "gowhere " << go[now][1] << ' ' << go[now][2] << endl;
		cout << "therehigh " << city[go[now][1]].h << ' ' << city[go[now][2]].h << endl;
	}
	cout << "checkone end now" << endl;
}

void basetwo()
{
	for(int i = 1; i <= n; i++)
	{
		int fir = go[i][2] , sec = go[fir][1];
		f[0][i] = sec;
		pas1[0][i] = abs(city[i].h - city[fir].h);
		pas2[0][i] = abs(city[fir].h - city[sec].h);
		cost[0][i] = pas1[0][i] + pas2[0][i];
	}
	for(int i = 1; i <= 20; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			f[i][j] = f[i - 1][f[i - 1][j]];
			pas1[i][j] = pas1[i - 1][j] + pas1[i - 1][f[i - 1][j]];
			pas2[i][j] = pas2[i - 1][j] + pas2[i - 1][f[i - 1][j]];
			cost[i][j] = pas1[i][j] + pas2[i][j];
		}
	}
}

void checktwo()
{
	cout << "checktwo now" << endl;
	for(int i = 1; i <= n; i++)
	{
		int now = ord[i];
		cout << now << ' ' << f[0][now] << ' ' << cost[0][now] << endl;
	}
	cout << "checktwo end now" << endl;
}

void drive(int sta , int x)
{
	dis1 = dis2 = 0;
	int now = sta , ret , flag = 0;
	while(!flag)
	{	
		ret = now , flag = 1;
		for(int k = 20; k >= 0; k--)
		{
			if(cost[k][now] > x || !f[k][now] || f[k][now] > n)
				continue;
			dis1 += pas1[k][now] , dis2 += pas2[k][now];
			x -= cost[k][now] , now = f[k][now];
			break;
		} 	
		if(ret != now) flag = 0;
	}
	if(x >= pas1[0][now] && f[0][now] > 0 && f[0][now] <= n) dis1 += pas1[0][now];
}


int main()
{
	cin >> n; 
	for(int i = 1; i <= n; i++)
	{
		cin >> city[i].h;
		city[i].num = i;
	}
	sort(city + 1 , city + n + 1);
	baseone();// , checkone();
	basetwo();// , checktwo();
	int x , ans = 0 , sta;
	long long reta = 0 , retb = 0;
	cin >> x;
	drive(ord[1] , x);
	//cout << dis1 << ' ' << dis2 << endl;
	ans = 1 , reta = dis1 , retb = dis2;
	if(retb == 0) reta = INF;
	for(int i = 2; i <= n; i++)
	{
		drive(ord[i] ,  x);
		//cout << dis1 << ' ' << dis2 << endl;
		if(!dis2) dis1 = INF;
		//dis1 / dis2 < reta / retb
		if(dis1 * retb <= dis2 * reta) 
			ans = i , reta = dis1 , retb = dis2;
	}
	cout << ans << endl;
	cin >> m;
	for(int i = 1; i <= m; i++)
	{
		cin >> sta >> x;
		drive(ord[sta] , x);
		cout << dis1 << ' ' << dis2 << endl;
	}
	return 0;
}
CPP
4 
2 3 1 4 
3 
4 
1 3 
2 3 
3 3 
4 3

回复

1 条回复,欢迎继续交流。

正在加载回复...