A. Two Rabbits
题意:数轴上有x,y,且x<y。x可以每次+a,y可以每次-b。问能否xy相遇。
思路:只要xy差值是a+b的倍数即可。
view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll x, y, a, b;
cin>>x>>y>>a>>b;
if((y-x)%(a+b)==0) cout<<(y-x)/(a+b)<<endl;
else cout<<-1<<endl;
}
return 0;
}
B. Longest Palindrome
题意:给n个不同的字符串,问你删除掉若干个之后,然后把这些字符串任意拼接,能否组成一个回文串。
思路:一开始以为各个字符也能打乱卡了一下,后面才发现只是字符串直接打乱重组。
这样其实很好想了。首先我们考虑如果这个字符串本身是回文串的话,因为n个字符串互不相同,那么肯定找不出另一个跟它匹配的。所以如果存在,只能放在中间。
其次就是两两搭配,如果串a和串b放在两边能构成回文,就加进去。写的时候用一个head和tail的vector存一下结果就行了。
view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
bool vis[505];
bool check(string s)
{
for(int i=0, j=s.size()-1; i<=j; i++, j--)
if(s[i]!=s[j]) return false;
return true;
}
int main()
{
ll n, m;
cin>>n>>m;
vector<string> s;
vector<string> head;
vector<string> tail;
int flag = 1;
int cnt = 0;
string mid;
rep(i,1,n)
{
string item;
cin>>item;
s.pb(item);
if(check(item))
cnt++, mid = item;
}
for(int j=0; j<n; j++)
{
if(vis[j]) continue;
for(int k=j+1;k<n;k++)
{
if(vis[k]) continue;
string tmp = s[j] + s[k];
if(check(tmp))
{
head.pb(s[j]), tail.pb(s[k]), vis[j] = vis[k] = 1;
break;
}
}
}
if(cnt) head.pb(mid);
int len = 0;
for(int i=0; i<head.size(); i++) len += head[i].size();
for(int i=0; i<tail.size(); i++) len += tail[i].size();
cout<<len<<endl;
for(int i=0; i<head.size(); i++) cout<<head[i];
for(int i=tail.size()-1; i>=0; i--) cout<<tail[i];
cout<<'
';
return 0;
}
C. Air Conditioner
题意:给你n个区间,每个区间有一个时刻,刚开始你的数为m,你可以每过1个单位时间就+1或-1,问你能否使得每个区间时刻到达时满足m在区间范围里。
思路:贪心。我们遍历到每个区间的时候,就先得到在满足前面所有区间的情况下,能达到的最短点最小值和右端点最大值。
然后看看当前区间与能得到的区间有无交集即可。满足的话继续更新这个最大范围区间
view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
typedef struct Information
{
ll t;
ll l;
ll r;
}I;
I a[500];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read(), m = read();
rep(i,1,n) a[i].t = read(), a[i].l = read(), a[i].r = read();
int flag = 1;
ll mi = m, ma = m; //mi和ma记录当前区间能达到的最大范围的左右端点
ll t = 0; //刚开始时刻为0
for(int i=1;i<=n;i++)
{
ll curL = a[i].l, curR = a[i].r; //当前时刻的满足约束的最大区间
rep(j,i+1,n)
{
if(a[i].t != a[j].t)
{
i = j-1;
break;
}
curL = max(curL,a[j].l), curR = min(curR, a[j].r); //更新约束
}
if(a[i].t==a[n].t) i=n; //后面都是一个时刻了
if(curL>curR||curL>ma+a[i].t - t||curR<mi - a[i].t + t) //如果当前约束区间合法且能够由之前的满足题意区间达到就行(有交集),不然就直接break。
{
flag = 0;
break;
}
mi = max( mi - a[i].t + t, curL), ma = min(ma + a[i].t - t, curR); //更新最大范围
t = a[i].t;
}
puts(flag?"YES":"NO");
}
return 0;
}