Codeforces Round #129 (Div. 2)
A水题
B题,我用的是单调栈来搞的,思路:从右向左扫描,维护一个单调递减的栈,如果新加入的元素比栈顶元素高的话,就ans+=差值
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=100010;
int t[maxn],stack[maxn];
int main()
{
int n;
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
scanf("%d",&t[i]);
int top=0;
long long ans=0;
for(int i=n;i>=1;i--)
{
if(top>0&&t[i]>=stack[top-1])
{
// cout<<t[i]<<" "<<stack[top-1]<<endl;
ans+=(t[i]-stack[top-1]);
while(top>0&&t[i]>=stack[top-1]) top--;
}
stack[top++]=t[i];
}
printf("%I64d\n",ans);
}
return 0;
}
/*
10
50 13 9 3 5 6 7 4 1 47
*/
C题,如果给定数的位数为len,那么先求出1,2,len-1位的满足题意的个数,然后再求len位的个数,例如4313
那么 1位数的个数 : 9
2位数的个数: 9
3位数的个数: 90
四位数个数 :
首位数是1,2,3时 3*100
首位数是 4时 31 - 1
#include <iostream>
using namespace std;
long long a[20];
void init ( )
{
a[1] = 9;
a[2] = 9;
for ( int i = 3; i < 20; ++i )
a[i] = a[i - 1] * 10;
}
long long solve(long long x )
{
long long ret = 0, tmp, tmp1;
int len = 0, b[18], i;
if ( x == 0 ) return 0;
while ( x )
{
b[len++] = x % 10;
x /= 10;
}
if ( len == 1 ) return b[0];
for ( i = 1; i < len; ++i ) ret += a[i];
//cout<<ret<<endl;
tmp1 = b[len - 1] - 1;
//cout<<tmp1<<endl;
tmp = len - 2;
while ( tmp-- ) tmp1 *= 10;
//cout<<tmp1<<endl;
ret += tmp1;
for ( tmp = 0, i = len - 2; i > 0; --i ) tmp = tmp*10 + b[i];
tmp++;
if ( b[0] >= b[len - 1] ) tmp = tmp;
else tmp--;
ret += tmp;
return ret;
}
int main ( )
{
int i, len1, len2;
init( );
long long l, r;
while( cin >> l >>r)
{
cout << solve ( r ) - solve(l-1) << endl;
}
}
D题 离散化直接用map更方便
#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
map<int,pair<int,int> > h;
map<int,pair<int,int> >::iterator it;
int main()
{
int n,a,b;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
h[a].first++;
if(a!=b) h[b].second++;
}
// for(it=h.begin();it!=h.end();it++)
// cout<<(*it).first<<" "<<(*it).second.first<<" "<<(*it).second.second<<endl;
n=(n+1)/2;
int ans=99999999;
for(it=h.begin();it!=h.end();it++)
if((*it).second.first+(*it).second.second>=n) ans=min(ans,(n-(*it).second.first)<0?0:(n-(*it).second.first));
if(ans!=99999999) printf("%d\n",ans);
else puts("-1");
return 0;
}