两点之间的距离每次减少 a+b,初始时距离为 y-x
如果两个人恰好遇到,那么 (y-x)%(a+b)==0
时间为 (y-x)/(a+b)
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
void solve()
{
int x,y,a,b;
cin>>x>>y>>a>>b;
if((y-x)%(a+b))cout<<-1<<endl;
else cout<<(y-x)/(a+b)<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
思路:暴力枚举,首先先找出自身回文得放在中间,只能有一个
然后存储互相回文得字符出输出即可
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=110;
vector<string>v;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
bool check(string s)
{
string t=s;
reverse(all(t));
return s==t;
}
void solve()
{
cin>>n>>m;
string s;
map<string,bool>mp;
vector<string>v1,v2;
string mid="";
for(int i=0;i<n;i++)
{
cin>>s;
v.pb(s);
}
for(int i=0;i<n;i++)
{
if(check(v[i]))
{
mid=v[i];
continue;
}
for(int j=0;j<n;j++)
{
if(j==i)continue;
string t=v[i]+v[j];
if(check(t)&&mp[v[i]]==false&&mp[v[j]]==false)
{
v1.pb(v[i]);
v2.pb(v[j]);
mp[v[i]]=mp[v[j]]=true;
}
}
}
int len=v1.size()+v2.size();
if(mid!="")len++;
cout<<len*m<<endl;
for(int i=0;i<v1.size();i++)cout<<v1[i];
if(mid!="")cout<<mid;
for(int i=v2.size()-1;i>=0;i--)cout<<v2[i];
cout<<endl;
}
signed main()
{
io;
// cin>>_;
// while(_--)
solve();
return 0;
}
思路:贪心,维护区间
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=110;
struct node
{
int t,l,r;
}c[N];
void solve()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>c[i].t>>c[i].l>>c[i].r;
bool f=false;
int l=m,r=m;
for(int i=1;i<=n;i++)
{
int d=c[i].t-c[i-1].t;
l-=d,r+=d;
if(l>c[i].r||r<c[i].l)
{
f=true;
break;
}
l=max(l,c[i].l),r=min(r,c[i].r);
}
if(f)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}