A
简单减法
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include<bits/stdc++.h> using namespace std; #define ll long long int main(){ int l,r; l=r=0; string s; int n; scanf("%d",&n); cin>>s; for(int i=0;i<n;i++) { if(s[i]=='L')l--; else r++; } printf("%d\n",r-l+1); }
|
B
思路
从0-n-1,1-n找最大的区间和即可
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include<bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5+10; int a[N]; int main() { int t; scanf("%d",&t); while(t--) { bool flag = false; bool all = true; int n; scanf("%d",&n); ll temp = 0; ll sum = 0; ll maxsum = -1e18; for(int i=0;i<n;i++)scanf("%d",&a[i]),sum+=a[i]; for(int i=1;i<n;i++) { if(a[i]<=0)flag=true; temp+=a[i]; if(temp<0)temp=0,all=false; maxsum = max(maxsum,temp); } temp=0; for(int i=0;i<n-1;i++) { if(a[i]<=0)flag=true; temp+=a[i]; if(temp<0)temp=0,all=false; maxsum = max(maxsum,temp); } if(!flag) { printf("YES\n"); continue; } if(maxsum>=sum)printf("NO\n"); else printf("YES\n"); } }
|
C
思路
直接暴力sqrt(n)枚举,判断即可
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include<bits/stdc++.h> using namespace std; #define ll long long ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; } int main() { ll x; scanf("%lld",&x); ll a=1,b=x; ll maxx = x; ll sx = ceil(sqrt(x)); for(int i=2;i<sx;i++) { if(x%i==0&&(x%(1ll*i*i)!=0)){ ll aa = i; ll bb = x/i; ll temp = aa*bb/gcd(aa,bb); if(temp==x&&max(aa,bb)<maxx)a=aa,b=bb; } } printf("%lld %lld\n",a,b); }
|
D
思路
把数字转换为二进制之后一列一列的看,如果这一列的0与1小于等于1个的话那么就加上(1<<bits),如果为0则不管。
递归的往下搜索即可
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include<bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5+10; bool vis[N]; vector<int>a; ll solve(vector<int> &c,int bits) { if(bits<0)return 0; vector<int>l,r; for(auto it : c) { if(((1<<bits)&it)==0)l.push_back(it); else r.push_back(it); } if(l.size()==0)return solve(r,bits-1); if(r.size()==0)return solve(l,bits-1); return min(solve(l,bits-1),solve(r,bits-1))+(1<<bits); } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { int x; scanf("%d",&x); a.push_back(x); } printf("%lld\n",solve(a,29)); }
|