CodeForces - 1465E Poman Numbers(推公式+贪心)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1465E Poman Numbers(推公式+贪心)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個長度為 nnn 的字符串,每個字符串實質上代表一個數字,例如:a=20,b=21,?,z=225a=2^0,b=2^1,\cdots,z=2^{25}a=20,b=21,?,z=225,現在需要尋找一種遞歸順序,使得每個位置非負即正,遞歸規則如下:
f(S)=?f(S[1,m])+f(S[m+1,∣S∣])f(S) = -f(S[1, m]) + f(S[m + 1, |S|]) f(S)=?f(S[1,m])+f(S[m+1,∣S∣])
其中 mmm 是 [l,r][l,r][l,r] 中任意一個位置
現在問能否通過某種遞歸順序,使得整個序列之和為給定的值 sumsumsum
題目分析:結論:第 nnn 個位置的符號一定為正,第 n?1n-1n?1 個位置的符號一定為負,其余 n?2n-2n?2 個位置的符號隨意
簡單證明,用數學歸納法,設最終的符號位為 a1,a2,?,ana_1,a_2,\cdots,a_na1?,a2?,?,an?,其中 ai=?1a_i=-1ai?=?1 或 ai=1a_i=1ai?=1:
代碼:
// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<unordered_map> using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; char s[N]; int n,cnt[N]; LL f[N],sum; void init() {f[0]=1;for(int i=1;i<26;i++) f[i]=f[i-1]*2; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);init();read(n),read(sum);scanf("%s",s+1);sum-=f[s[n]-'a']-f[s[n-1]-'a'];for(int i=1;i<=n-2;i++) sum+=f[s[i]-'a'],cnt[s[i]-'a']++;for(int i=25;i>=0;i--) while(cnt[i]&&sum>=2*f[i]) sum-=2*f[i],cnt[i]--;puts(sum?"No":"Yes");return 0; }總結
以上是生活随笔為你收集整理的CodeForces - 1465E Poman Numbers(推公式+贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1207F R
- 下一篇: 2020ICPC(南京) - Just