CodeForces - 1260B
题意:给出两个数,可进行多次操作,每次操作选中一个正数x,一个数减去x,另一个减去2*x,问经过多次操作是否可以将两个数同时变为0.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#define INF 0x3f3f3f3f
#define mod 2008
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
bool f=0;
int a,b,xa,xb;
cin>>xa>>xb;
a=min(xa,xb);//=k+2m
b=max(xa,xb);//=m+2k
if(b>2*a) cout<<"NO"<<endl;
else
{
if((a+b)%3) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
return 0;
}
当a<b,设a=k+2m,b=2k+m,最开始是穷举m超时了,然后解方程根据a,b推k,m,再代入b看是否满足,错在解方程反推k,m的前提是数据已经符合要求,直接求会因为(a+b)%3!=0的数据得到不该得到的k值,而又凑巧满足判定条件,正确思路如上。
CodeForces - 1260C
题意:无限长的分块篱笆,给出r,b,k三个数,r的倍数块涂一个色,b的倍数快涂另一个色,同时是r和b的倍数可以涂任一色,涂完忽略没有上色的,问相同颜色连续的块会不会超过K,
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#define INF 0x3f3f3f3f
#define mod 2008
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int r,b,k;
cin>>r>>b>>k;
if(r>b) swap(r,b);
long long s=(b-1-__gcd(r,b))/r+1;
if(s>=k) cout<<"REBEL"<<endl;
else cout<<"OBEY"<<endl;
}
return 0;
}
当时没做出来,事后花了很长时间才搞懂,在数论方面还要多下功夫,看图比较好理解
k1b+t=x1r是裴蜀等式,根据裴蜀定理t是gcd(r,b)的倍数,因此t最小为gcd(r,b),+1是因为k1b到(k1+1)b之间(不包括首尾)是b-1个数,减去t之后x1r也不在范围内(类似开边界的端点),所以要加1将x1补上。
CodeForces - 1260D *
题意:给出四个数:1.M士兵数,每个士兵有对应的敏捷值,可以翻过危险度低于他的陷阱,2.N到BOSS的距离0~N+1,3.K陷阱个数,每个陷阱对应三个值Li陷阱坐标,Di陷阱危险度,Ri自己走到该坐标可以使陷阱失效
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
int m, n, k, t;
int a[MAX_N], l[MAX_N], r[MAX_N], d[MAX_N];
bool ok(int val) {
vector<int> v(n + 1);
for (int i = 0; i < k; i++) if (d[i] > val) {
v[l[i] - 1]++;
v[r[i]]--;
}
for (int i = 1; i <= n; i++) v[i] += v[i - 1];
int ans = n + 1;
for (int i = 0; i <= n; i++) if (v[i]) ans += 2;
return ans <= t;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> m >> n >> k >> t;
for (int i = 0; i < m; i++) cin >> a[i];
for (int i = 0; i < k; i++) cin >> l[i] >> r[i] >> d[i];
int lf = 0, rt = MAX_N, rs = -1;
while (lf <= rt) {
int mid = (lf + rt) >> 1;
if (ok(mid)) rs = mid, rt = mid - 1;
else lf = mid + 1;
}
int ans = 0;
for (int i = 0; i < m; i++) if (a[i] >= rs) ans++;
cout << ans;
return 0;
}
二分法,对时间要求很高,>>1改成*2就会超时
CodeForces - 1260E *
题意:n个拳手,下标是力量值,即序号越打越强,花ai钱贿赂第i个人取胜,通过分配比赛的组队,以最少的钱取得胜利
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+5;
ll a[N];
multiset<ll>s;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
ll ans=0;
for(int i=n;a[i]!=-1;i--){
s.insert(a[i]);
if(!(i&(i-1))){
ans+=*s.begin();
s.erase(s.begin());
}
}
printf("%lld",ans);
return 0;
}
使用优先队列,multisets建立一个multiset类型,变量名是s,s序列里面存的是long long类型,初始s为空,保证序列中的数是有序的,并且数可以重复
CodeForces - 1260F *
题意:给一颗n个节点的树,两个同色点之间的路径和表示树的价值