题目大意:
一个城市有m个城市,每个城市先投票选出一个候选人,得票多的胜出,得票相同时,下标小的胜出,接着在城市
选出的候选人中选出一个胜者。
解题思路:
按照题意模拟就好了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=15000;
int d[maxn];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int maxx=-1,maxi=0;
int x;
for(int j=1;j<=n;j++)
{
scanf("%d",&x);
if(x>maxx)
{
maxx=x;
maxi=j;
}
}
//cout<<maxi<<endl;
d[maxi]++;
}
int ans=1;
for(int i=1;i<=n;i++)
{
if(d[i]>d[ans])
{
ans=i;
}
}
printf("%d\n",ans);
return 0;
}
题目大意:
两人在1~n内任选出一个数,再随机产生一个1~n的数,两人谁的离这个数相近就胜出,现在一人已经选了m,且两人距离相同时这人胜出,求另一个人选哪个数有最大的概率胜出。
解题思路:
如果m>n/2,肯定选m-1,如果m<n/2,肯定选m+1,n=1时要特殊处理。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int n,m,ans;
scanf("%d%d",&n,&m);
if(m>n/2)
ans=m-1;
else
ans=m+1;
if(n==1)
ans=1;
cout<<ans<<endl;
return 0;
}
题目大意:
其实就是统计每次改变字符串内的一个元素,还有多少个不同的子串".."。
解题思路:
先统计有多少个"..",接着进行取代操作,这是有2种情况,'.'取代字母,字母取代'.',无论哪种情况,我们只需
考虑操作的左边与右边的情况,拿'.'取代字母的情况,如果'.'的左边是'.‘,则多了1种'..'的子串,如果右边也是'.',也
多了一种。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=300000+1000;
char s[maxn];
int main()
{
int n,q;
scanf("%d%d",&n,&q);
scanf("%s",s+1);
int ans=0;
int cur=0;
for(int i=1;i<=n;i++)
{
if(s[i]=='.')
{
cur++;
}
else
{
if(cur>0)
{
ans+=(cur-1);
}
cur=0;
}
}
if(cur>0)
ans+=(cur-1);
for(int i=0;i<q;i++)
{
int x;
char op[5];
scanf("%d%s",&x,op);
if(op[0]=='.')
{
if(s[x]!='.')
{
int temp=0;
if(x>1)
{
if(s[x-1]=='.')
temp++;
}
if(x<n)
{
if(s[x+1]=='.')
temp++;
}
ans+=temp;
s[x]='.';
}
}
else
{
if(s[x]=='.')
{
int temp=0;
if(x>1)
{
if(s[x-1]=='.')
temp++;
}
if(x<n)
{
if(s[x+1]=='.')
temp++;
}
ans-=temp;
s[x]=op[0];
}
}
printf("%d\n",ans);
}
return 0;
}