注意特判相等的情况
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
int n;
string s;
string w;
int main()
{
// freopen("data.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin >> s;
cin >> n;
int f=0,f1=0;
for(int i=0;i<n;i++)
{
cin >> w;
if(w==s)
{
f=1,f1=1;
}
if(w[0]==s[1])
{
f1=1;
}
if(w[1]==s[0])
{
f=1;
}
}
if(f1&&f)
{
cout <<"YES"<<endl;
}
else
{
cout <<"NO"<<endl;
}
return 0;
}
用把三个指针都转换为60格的,然后暴力枚举
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
int h,m,s;
int t1,t2;
int main()
{
// freopen("data.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin >> h>>m>>s>>t1>>t2;
h*=5;
h%=60;
t1*=5;
t1%=60;
t2*=5;
t2%=60;
double ss = s;
double mm = m+s/60.0;
double hh = h+mm/60.0*5;
int f=0;
for(int i=t1;;i=(i+1)%60)
{
if(i==t1)continue;
if(i==0)
{
if(ss>=59||mm>=59||hh>=59)
break;
}
else
{
if(ss>=i-1&&ss<=i || mm>=i-1&&mm<=i || hh>=i-1&&hh<=i)
break;
}
if(i==t2)
{
f=1;
break;
}
}
for(int i=t1;;i=(i-1+60)%60)
{
if(i==t1)continue;
if(i==59)
{
if(ss>=59||mm>=59||hh>=59)
break;
}
else
{
if(ss>=i&&ss<=i+1 || mm>=i&&mm<=i+1 || hh>=i&&hh<=i+1)
break;
}
if(i==t2)
{
f=1;
break;
}
}
if(f)
{
cout <<"YES"<<endl;
}
else
{
cout <<"NO"<<endl;
}
return 0;
}
贪心想一下,其实只要一个或者两个就够了,不需要太多,这样暴力枚举,二分查找就OK
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
int dp[100005][(1<<4)];
int n;
int a[100005][4];
int m;
vector<int> b;
int main()
{
// freopen("data.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin >> n>>m;
for(int i=0;i<n;i++)
{
int tp=0;
for(int j=0;j<m;j++)
{
cin >> a[i][j];
if(a[i][j])
tp += (1<<j);
}
b.push_back(tp);
}
int maxx = (1<<m);
if(n==1)
{
int ct =0;
for(int i=0;i<m;i++)
{
ct += a[0][i];
}
if(ct==0)
{
cout <<"YES"<<endl;
}
else
{
cout << "NO"<<endl;
}
return 0;
}
sort(b.begin(),b.end());
for(int i=0;i<n;i++)
{
for(int j=b[i];j<maxx;j++)
{
int f= 0;
for(int k=0;k<m;k++)
{
if( ( ((b[i]>>k)&1) ==1 ) && ( ((j>>k)&1) ==0 ) )
{
f=1;
break;
}
}
if(f) continue;
int t1= j-b[i];
if( binary_search(b.begin()+i+1,b.end(),t1) )
{
// cout << b[i]<<" "<< t1<<endl;
cout <<"YES"<<endl;
return 0;
}
}
}
cout <<"NO"<<endl;
return 0;
}