2017-10-湖南套题2
处理0的前缀和,枚举第i位不变,[1,i-1]全变为0,[i+1,n]全变为1的最小代价
1 #include <cstring> 2 #include <cstdio> 3 4 const int N(1e5+5); 5 int sum[N],ans,tmp; 6 char s[N]; 7 8 int Presist() 9 { 10 freopen("reverse.in","r",stdin); 11 freopen("reverse.out","w",stdout); 12 scanf("%s",s); int n=strlen(s); 13 for(int i=1; i<=n; ++i) 14 sum[i]=sum[i-1]+(s[i-1]=='0'); 15 ans=N; 16 for(int i=1; i<=n; ++i) 17 { 18 tmp=i-1-sum[i-1]; 19 tmp+=sum[n]-sum[i]; 20 ans=ans<tmp?ans:tmp; 21 } 22 printf("%d\n",ans); 23 return 0; 24 } 25 26 int Aptal=Presist(); 27 int main(int argc,char**argv){;}
用hash的思想,出现字符相同的[1,n]中的每个数放到同一个集合里,组合数统计答案
1 #include <algorithm> 2 #include <cstdio> 3 4 #define LL long long 5 6 inline void read(int &x) 7 { 8 x=0; register char ch=getchar(); 9 for(; ch>'9'||ch<'0'; ) ch=getchar(); 10 for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0'; 11 } 12 13 const int N(1e7+5); 14 int p[10]={97,89,83,79,73,71,67,61,59,53}; 15 bool vis[N][10]; 16 LL cnt[N],ans,maxn; 17 int n; 18 19 int Presist() 20 { 21 // freopen("number.in","r",stdin); 22 // freopen("number.out","w",stdout); 23 read(n); 24 for(int t,x,i=1; i<=n; ++i) 25 { 26 LL tmp=0; 27 for(x=i; x; x/=10) 28 { 29 t=x%10; 30 if(!vis[i][t]) tmp+=(t+101)*p[t]; 31 vis[i][t]=1; 32 } 33 maxn=maxn>tmp?maxn:tmp; 34 cnt[tmp]++; 35 } 36 for(int i=101; i<=maxn; ++i) 37 if(cnt[i]>1) ans+=cnt[i]*(cnt[i]-1)>>1; 38 printf("%I64d\n",ans); 39 return 0; 40 } 41 42 int Aptal=Presist(); 43 int main(int argc,char**argv){;}
1 #include<cstdio> 2 #define maxn 2000 3 using namespace std; 4 int n,v[maxn]; 5 long long ans; 6 int main() 7 { 8 freopen("number.in","r",stdin); 9 freopen("number.out","w",stdout); 10 int a,b,x; 11 scanf("%d",&n); 12 for(int i=1;i<=n;i++) 13 { 14 a=i; 15 x=0; 16 while(a) 17 { 18 b=a%10; 19 x|=1<<b; 20 a=a/10; 21 } 22 ans+=v[x]; 23 v[x]++; 24 } 25 printf("%I64d\n",ans); 26 return 0; 27 }
贪心:满足相邻两个数的差>=k,偶数项尽量大,奇数项尽量小
1 #include <cstring> 2 #include <cstdio> 3 4 inline void read(int &x) 5 { 6 x=0; register char ch=getchar(); 7 for(; ch>'9'||ch<'0'; ) ch=getchar(); 8 for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0'; 9 } 10 const int N(2e6+5); 11 int n,k,a[N]; 12 13 int Presist() 14 { 15 // freopen("wave.in","r",stdin); 16 // freopen("wave.out","w",stdout); 17 read(n),read(k); 18 for(int i=1; i<=n; ++i) read(a[i]); 19 int op=0,pre=a[1],ans=1; 20 for(int i=2; i<=n; ++i) 21 { 22 if(op) 23 if(pre-a[i]>=k) 24 op=0,pre=a[i],ans++,printf("%d ",pre); 25 else pre=pre>a[i]?pre:a[i]; 26 else if(a[i]-pre>=k) 27 op=1,pre=a[i],ans++,printf("%d ",pre); 28 else pre=pre<a[i]?pre:a[i]; 29 } 30 printf("%d\n",ans); 31 return 0; 32 } 33 34 int Aptal=Presist(); 35 int main(int argc,char**argv){;}
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。