今天这场A了4道题,哈哈哈哈,虽然题目水的不行,还是挺开心的,而且有一道题是最后一分钟AC。。。先贴代码
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 1005
int n;
int a[M];
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
int ans=0;
for(i=2;i<n;i++)
{
if(a[i]>a[i-1]&&a[i]>a[i+1])
ans++;
else if(a[i]<a[i-1]&&a[i]<a[i+1])
ans++;
}
printf("%d\n",ans);
return 0;
}
Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform:
- U — move from the cell (x, y) to (x, y + 1);
- D — move from (x, y) to (x, y - 1);
- L — move from (x, y) to (x - 1, y);
- R — move from (x, y) to (x + 1, y).
Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!
The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).
The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.
Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.
4 LDUR
4
5 RRRUU
0
6 LLRRRR
4
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 105
int n,ans;
int num[5];
char s[M];
int main()
{
int i;
scanf("%d",&n);
scanf("%s",s);
for(i=0;i<n;i++)
{
if(s[i]=='L')
num[1]++;
else if(s[i]=='R')
num[2]++;
else if(s[i]=='U')
num[3]++;
else if(s[i]=='D')
num[4]++;
}
ans=min(num[1],num[2])*2+min(num[3],num[4])*2;
printf("%d\n",ans);
return 0;
}
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
const int maxval=10000000000;
int len,ans;
int num[28];
int pre[28];
char s[M];
int main()
{
int i,j;
char ch='a';
scanf("%s",s);
len=strlen(s);
for(i=0;i<26;i++)
{
pre[i]=-1;
num[i]=-1;
}
for(i=0;i<len;i++)
{
int it=s[i]-'a';
if(pre[it]!=-1)
num[it]=max(num[it],i-pre[it]);
else
num[it]=i+1;
pre[it]=i;
}
for(i=0;i<26;i++)
{
num[i]=max(num[i],len-pre[i]);
}
ans=maxval;
for(i=0;i<26;i++)
{
if(num[i]!=0)
ans=min(ans,num[i]);
}
printf("%d\n",ans);
return 0;
}
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
ll ans;
ll v[6]={0,0,1,2,9};
ll p[1005][5];
void init(ll i,ll t)
{
ll j,k,temp1,temp2;
for(j=1;j<=t;j++)
{
temp1=temp2=1;
for(k=0;k<j;k++)
temp1=temp1*(i-k);
for(k=1;k<=j;k++)
temp2=temp2*k;
p[i][j]=temp1/temp2;
}
}
int main()
{
ll i,n,k;
ll ans=0;
scanf("%I64d",&n);
scanf("%I64d",&k);
init(n,k);
ans=1;
for(i=2;i<=k;i++)
ans+=p[n][i]*v[i];
printf("%I64d\n",ans);
return 0;
}