Codeforces Round 918 (Div. 4)--E~F

E. Romantic Glasses

Codeforces Round 918 (Div. 4)--E~F_第1张图片

思路:偶数减奇数的前缀和,如果出现重复的数字,则该相同的值两个位置满足 

#include
using namespace std;
using ll = long long;
void solve()
{
	ll n;cin>>n;
	vectora(n+1);
	ll sum=0,x=0;
	setd;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		if(i&1) sum+=-a[i];
		else sum+=a[i];
		if(sum==0) x++;
		d.insert(sum);
	}
	ll s=d.size();
	if(s==n&&x==0) cout<<"NO"<<'\n';
	else cout<<"YES"<<'\n';
}
int main()
{
	ll t;cin>>t;
	while(t--) solve();
	return 0;
}

F. Greetings

Codeforces Round 918 (Div. 4)--E~F_第2张图片

思路:将两点存到一起,然后按起点从低排序,从小枚举每个起点的终点前有几个点,然后删除该点即可!!

#include
using namespace  std;
using ll = long long;

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	int t; cin >> t;
	while(t--) {
		int n; cin >> n;
		vector> p(n);
		vector v(n);
		
		for(int i = 0; i < n; i++) {
			int x, y; cin >> x >> y;
			p[i] = {x,y};
			v[i] = y;
		}
		
		sort(p.begin(), p.end());
		sort(v.begin(), v.end());
		
		ll ans = 0;
		for (int i = 0; i < n; i++) {
			int x = p[i].second;
			int k = lower_bound(v.begin(), v.end(), x) - v.begin();
			ans += k;
			v.erase(v.begin() + k, v.begin() + k + 1);
		}
		
		cout << ans << '\n';
	}
	return 0;
}

你可能感兴趣的:(CF比赛(练习),c++,算法,数据结构)