<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int ok=1;
while(n+m>1&&n>0&&m>0)
{
n--;
m--;
ok++;
}
if(ok&1)
printf("Malvika\n");
else
printf("Akshat\n");
}
return 0;
}</span>
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.
Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a.
The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer.
If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
3
3 2 1
yes
1 3
4
2 1 3 4
yes
1 2
4
3 1 2 4
no
2
1 2
yes
1 1
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.
Sample 3. No segment can be reversed such that the array will be sorted.
Definitions
A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].
If you have an array a of size n and you reverse its segment [l, r], the array will become:
a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
题意:题目的意思是只交换一列数中的两个数能否实现数列有序。做法是先得到有序的数列,然后和原有数列进行比较,判断是否只有两个位置数字被交换。
<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e5+10;
int a[maxm];
struct node
{
int x,id;
} t[maxm];
int cmp(node p,node q)
{
return p.x<q.x;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0; i<n; i++)
{
scanf("%d",&t[i].x);
t[i].id=i+1;
}
sort(t,t+n,cmp);
int sum=0;
for(int i=0; i<n; i++)
{
a[i]=t[i].id;
if(a[i]==(i+1))
{
sum++;
}
}
if(sum==n)
{
printf("yes\n");
printf("1 1\n");
}
else
{
int x=0,y=0;
for(int i=0; i<n; i++)
{
if(a[i]!=(i+1))
{
x=i;
for(int j=n-1; j>=i+1; j--)
{
if(a[j]!=(j+1))
{
y=j+1;
break;
}
}
break;
}
}
reverse(a+x,a+y);
int cnt=0;
for(int i=0; i<n; i++)
{
if(a[i]==(i+1))
cnt++;
}
if(cnt==n)
{
printf("yes\n");
printf("%d %d\n",x+1,y);
}
else
{
printf("no\n");
}
}
}
return 0;
}</span>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL __int64
int main()
{
LL t;
scanf("%I64d",&t);
while(t--)
{
LL n,k,d1,d2;
LL w1,w2,w3;
LL x1,x2,x3;
LL y1,y2,y3;
LL z1,z2,z3;
scanf("%I64d%I64d%I64d%I64d",&n,&k,&d1,&d2);
if((n%3))
{
printf("no\n");
}
else
{
LL p=n/3;
w1=(k+d2+2*d1);
w2=(k+d2-d1);
w3=(k-2*d2-d1);
x1=(k+2*d1-d2);
x2=(k-d1-d2);
x3=(k-d1+2*d2);
y1=(k-2*d1+d2);
y2=(k+d1+d2);
y3=(k+d1-2*d2);
z1=(k-2*d1-d2);
z2=(k+d1-d2);
z3=(k+d1+2*d2);
if(w1/3<=p&&w2/3<=p&&w3/3<=p&&w1/3>=0&&w2/3>=0&&w3/3>=0&&w1%3==0&&w2%3==0&&w3%3==0)
printf("yes\n");
else if(x1/3<=p&&x2/3<=p&&x3/3<=p&&x1/3>=0&&x2/3>=0&&x3/3>=0&&x1%3==0&&x2%3==0&&x3%3==0)
printf("yes\n");
else if(y1/3<=p&&y2/3<=p&&y3/3<=p&&y1/3>=0&&y2/3>=0&&y3/3>=0&&y1%3==0&&y2%3==0&&y3%3==0)
printf("yes\n");
else if(z1/3<=p&&z2/3<=p&&z3/3<=p&&z1/3>=0&&z2/3>=0&&z3/3>=0&&z1%3==0&&z2%3==0&&z3%3==0)
printf("yes\n");
else
printf("no\n");
}
}
return 0;
}
题意:若一个字符串删除相同元素后剩下的是回文串,则称之为好串。给出一个由a和b组成的字符串,求奇数元素的好子串数量和偶数元素的好子串数量。
题解:DP统计。
注意字符串只有a和b,删除重复元素以后肯定是ababababab这样,两个a和之间的元素组成的串肯定是回文串,根本不用回文的算法。
由s[i]结尾的回文串数量等于0~i之间和s[i]相同字符的数量。要分奇数偶数长度,这和下标的奇偶有关。我们观察一下当前下标、之前的和当前字符相同的字符的下标、ans统计下标的关系(ans[0]记录偶数,ans[1]记录奇数)
now | 之前 | ans |
奇 | 奇 | 奇 |
奇 | 偶 | 偶 |
偶 | 奇 | 偶 |
偶 | 偶 | 奇 |
可以发现当前下标为奇数,当前元素为a,ans偶+=之前的偶数位置的a的数量,ans奇+=之前奇数位置a的数量。
当前下标为偶数,当前元素为a,ans偶+=之前的奇数位置的a的数量,ans奇+=之前偶数位置a的数量。
这样就总结出了比较简单的统计方法。(其实不总结,直接用if也可以,比赛时最好用直接点的方法,我这是事后做的才搞这种)
这样我们从头扫到尾,慢慢统计字符的数量和奇数偶数回文串的数量就行了。
<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL __int64
const int maxm=1e5+10;
char s[maxm];
int sumodd[5];
int sumeven[5];
int main()
{
while(scanf("%s",s)!=EOF)
{
memset(sumodd,0,sizeof(sumodd));
memset(sumeven,0,sizeof(sumeven));
LL odd=0,even=0;
for(int i=0;i<strlen(s);i++)
{
int t=s[i]-'a';
odd++;
if(i&1)
{
odd+=sumodd[t];
even+=sumeven[t];
sumodd[t]++;
}
else
{
odd+=sumeven[t];
even+=sumodd[t];
sumeven[t]++;
}
}
printf("%I64d %I64d\n",even,odd);
}
return 0;
}</span>