Codeforces Round #812 (Div. 2)

A. Traveling Salesman Problem
题目链接:
https://codeforces.com/contest/1713/problem/A
思路:
结果等于四个方向上绝对值之和的二倍
代码:

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int a[4];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		memset(a,0,sizeof a);
		int res=0;
		while(n--)
		{	
			int x,y;
			cin>>x>>y;
			if(x==0&&y>0)
			{
				a[0]=max(a[0],y);
			}
			else if(x==0 && y<0)
			{
				a[2]=max(a[2],abs(y));
			}
			else if(x>0 && y==0)
			{
				a[1]=max(a[1],x);
			}
			else if(x<0 && y==0)
			{
				a[3]=max(a[3],abs(x));
			}
		}

		res=a[0]*2+a[1]*2+a[2]*2+a[3]*2;
		cout<<res<<endl;

	}
}

B. Optimal Reduction
题目链接:https://codeforces.com/contest/1713/problem/B
思路: 如果出现凹型,则输出NO。否则输出YES
代码:

#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		vector<int>ve;
		for(int i=0;i<n;i++)
		{
			int t;
			cin>>t;
			if(i!=0 && t==ve[ve.size()-1])
			{
				continue;
			}
			ve.push_back(t);
		}
		bool f=true;
		for(int i=0;i<ve.size();i++)
		{
			int a=i-1;
			int b=i+1;
			if(a>=0 && b<ve.size())
			{
				if(ve[a]>ve[i] && ve[b]>ve[i])
				{
					f=false;
				}
			}
		}
		if(f)
		{
			cout<<"YES"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值