解题思路:
由题可知,其6个次为一个循环,所以把n对6取余,然后直接模拟
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
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;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e1+5;
typedef struct node{
int x;
int y;
int step;
}NODE;
int n;
int flag ;
int a[4];
int main()
{
cin >> n;
cin >> flag;
a[flag]=1;
n%=6;
for(int i=n;i>=1;i--)
{
if(i%2)
{
swap(a[0],a[1]);
}
else
{
swap(a[1],a[2]);
}
}
for(int i=0;i<3;i++)
{
if(a[i])
{
cout << i<<endl;
return 0;
}
}
return 0;
}
After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.
Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.
Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.
Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use.
The second line contains n digits — Sherlock's credit card number.
The third line contains n digits — Moriarty's credit card number.
First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.
3 123 321
0 2
2 88 00
2 0
First sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.
解题思路:
直接进行贪心,分别对两个排序,然后贪心求解。分别求不大于和大于。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
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;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e1+5;
typedef struct node{
int x;
int y;
int step;
}NODE;
int n;
string s,ss;
vi a,b;
queue<int> aq,bq,aqq,bqq;
int re ,re1;
int main()
{
int t,tt;
cin >> n;
cin >> s>>ss;
for(int i=0;i<n;i++)
{
a.push_back(s[i]-'0');
b.push_back(ss[i]-'0');
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
for(int i=0;i<n;i++)
{
aq.push(a[i]);
bq.push(b[i]);
}
while(!aq.empty())
{
if(bq.empty())
break;
t = aq.front();
aq.pop();
while(!bq.empty())
{
if(bq.front()<t)
{
re ++;
bq.pop();
}
else
break;
}
if(!bq.empty())bq.pop();
}
for(int i=0;i<n;i++)
{
aqq.push(a[i]);
bqq.push(b[i]);
}
while(!aqq.empty())
{
if(bqq.empty())
break;
t = aqq.front();
aqq.pop();
while(!bqq.empty())
{
if(bqq.front()<=t)
bqq.pop();
else
break;
}
if(!bqq.empty())
{
re1++;
bqq.pop();
}
}
cout <<re<<endl<<re1<<endl;
return 0;
}
直接用一个数组b,b[x]代表在左端为x时右端能达到的最大值为b[x],b[x]由暴力求。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
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;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e1+5;
typedef struct node{
int x;
int y;
int step;
}NODE;
int n,m,k;
vi a[100005];
int b[100005];
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin >>t;
a[i].push_back(t);
}
}
for(int j=0;j<m;j++)
{
a[n].push_back(0);
}
int st=0,end=0,tt;
for(int i=0;i<m;i++)
{
//cout << i<<":"<<endl;
if(b[0]==n-1)
break;
st=0;
end =st;
while(end<n-1)
{
end = st;
while(a[end][i]<=a[end+1][i])
end++;
for(int kk=st;kk<=end;kk++)
{
if(b[kk]<end)
b[kk] = end;
}
//cout << st<<" "<<end<<endl;
st = end+1;
}
}
int l,r;
cin >> k;
for(int i=0;i<k;i++)
{
cin >> l>>r;
l--,r--;
if(b[l]>=r)
{
cout <<"Yes"<<endl;
}
else
cout <<"No"<<endl;
}
return 0;
}
解题思路:
贪心,令最后一个长度最长,从后往前剪。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
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;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e1+5;
typedef struct node{
int x;
int y;
int step;
}NODE;
int n,m,k;
string s;
vector<string> a;
ll re = 0;
int main()
{
ios::sync_with_stdio(false);
cin >> n;
for(int i=0;i<n;i++)
{
cin >> s;
a.push_back(s);
}
int len;
int ff;
int j;
string ss =a[n-1];
for(int i=n-2;i>=0;i--)
{
ff = 0;
// cout << ss<< " "<< a[i]<<endl;
len = ss.size();
// cout << len<<endl;
for(j=0;j<len;j++)
{
if(ss[j]<a[i][j])
{
a[i] = a[i].substr(0,j);
break;
}
if(ss[j]>a[i][j])
{
ff = 1;
break;
}
}
if(j==len)
{
a[i] = a[i].substr(0,len);
}
if(len==1)
{
a[i] = a[i].substr(0,1);
}
ss = a[i];
}
for(int i=0;i<n;i++)
{
cout << a[i]<<endl;
}
return 0;
}