#677. 弗拉德和糖果 II |
思路:只要没有一个糖果数量大于总糖果数量的一半即可
代码:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL a[10000010];
LL n,sum=0;
int main(){
cin>>n;
for(LL i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
bool ans=1;
for(LL i=1;i<=n;i++){
if(a[i]*2>(sum+1)){
ans=0;
break;
}
}
cout<<(ans?"YES":"NO");
return 0;
}
思路:
上帝想加,那就把新来的减少一点就好了。优先队列处理
代码:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
priority_queue<LL,vector<LL>,greater<LL> >EVA;
LL les=0;
int main(){
LL T;
cin>>T;
while(T--){
LL op,x;
scanf("%lld",&op);
if(op==1){
scanf("%lld",&x);
x-=les;
EVA.push(x);
}
else if(op==2){
scanf("%lld",&x);
les+=x;
}
else{
if(!EVA.empty()){
printf("%lld\n",les+EVA.top());
EVA.pop();
}
}
}
return 0;
}
先求相对位置序列,然后题目就会转化为求最长上升子序列
代码:
#include<bits/stdc++.h>
using namespace std;
int n;
int a[100010],b[100010],wei_1[100010],wei[100100];
int dp[100010];
int maxx=0;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
wei_1[b[i]]=i;
}
//for(int i=1;i<=n;i++)cout<<wei_1[a[i]]<<" ";cout<<endl;
//for(int i=1;i<=n;i++)cout<<wei_2[b[i]]<<" ";cout<<endl;
for(int i=1;i<=n;i++){
wei[i]=wei_1[a[i]];
}
int pos=0;
dp[pos]=wei[1];
for(int i=2;i<=n;i++){
if(wei[i]>dp[pos]){
dp[++pos]=wei[i];
}
else{
dp[lower_bound(dp,dp+pos+1,wei[i])-dp]=wei[i];
}
}
cout<<pos+1;
return 0;
}
思路:
简单的求贡献即可
代码:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL a[1000010];
int main(){
LL n;
LL sum=0;
cin>>n;
for(LL i=1;i<=n;i++)cin>>a[i];
for(LL i=1;i<=n;i++){
LL ans_1=0;
for(LL j=1;j<i;j++){
if(a[j]<a[i])ans_1++;
}
LL ans_2=0;
for(LL j=i+1;j<=n;j++){
if(a[j]>a[i])ans_2++;
}
sum+=ans_1*ans_2;
}
cout<<sum;
return 0;
}
思路:看看前k位相等满不满足条件,若不满足,k位+1,循环输出即可
代码:
#include<bits/stdc++.h>
using namespace std;
int n,k,shu[200020];
int up=1e9;
int main(){
cin>>n>>k;
string str;
cin>>str;
int len=str.length();
for(int i=0;i<len;i++){
shu[i]=str[i]-'0';
}
int ans=0;
int wei=k+1;
for(int i=k;i<len;i++){
if(shu[i]!=shu[i%k]){
if(shu[i%k]>shu[i])break;
else{
shu[k-1]++;
break;
}
}
}
int ce=k-1;
while(shu[ce]==10)shu[ce-1]++,shu[ce]=0,ce--;
cout<<len<<endl;
for(int i=0;i<len;i++){
cout<<shu[i%k];
}
return 0;
}
思路:先判断同时删除同一个位置的字母会不会导致s1和s2相等(排除只有一个字母不同的情况)
接下来就是给每个字符串一个删字母的机会,注意判断特殊情况,详情见代码。
代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
string str1,str2;
cin>>str1>>str2;
int len1=str1.length();
int len2=str2.length();
if(len1!=len2){
cout<<"0";
return 0;
}
int l=0,r=0;
bool ans=1;
bool a=1,b=1;
for(int i=0;i<len1;i++){
if(str1[i]!=str2[i]){
char ch=str1[i];
str1[i]=str2[i];
if(str1==str2){
cout<<"1";
return 0;
}
else{
str1[i]=ch;
break;
}
}
}
while(l<len1 && r<len2 && ans){
if(str1[l]!=str2[r]){
if(a && b && str1[l+1]==str2[r] && str2[r+1]==str1[l]){
int lr=r,ll=l;
while(str1[ll]==str2[lr+1] && str2[lr]==str1[ll+1]){
ll++,lr++;
if(ll==len1)break;
}
if(ll==len1){
cout<<'1';
return 0;
}
if(str1[ll]!=str2[lr+1])l++,a=0;
if(str2[lr]!=str1[ll+1])r++,b=0;
}
else if(a&&str1[l+1]==str2[r])a=0,l++;
else if(b&&str2[r+1]==str1[l])b=0,r++;
else ans=0;
}
l++;
r++;
//cout<<l<<" "<<r<<" "<<ans<<endl;
}
cout<<(ans?1:0);
return 0;
}
思路:
BFS
代码:
#include<bits/stdc++.h>
using namespace std;
struct cjc{
int id;
int x,y;
};
int mapp[1000][1000];
bool book[1000][1000];
int maxx=0;
int n,m;
void BFS(int qx,int qy){
stack<cjc>EVA;
cjc wei;
wei.id=1;
wei.x=qx;
wei.y=qy;
book[qx][qy]=1;
EVA.push(wei);
while(!EVA.empty()){
wei=EVA.top();
maxx=max(maxx,wei.id);
EVA.pop();
cjc v;
v.id=wei.id+1;
v.x=wei.x+1;
v.y=wei.y;
if(book[v.x][v.y]==0 && mapp[v.x][v.y]==0 && v.x<=n && v.y<=m){
book[v.x][v.y]=1;
EVA.push(v);
}
v.x=wei.x;
v.y=wei.y+1;
if(book[v.x][v.y]==0 && mapp[v.x][v.y]==0 && v.x<=n && v.y<=m){
book[v.x][v.y]=1;
EVA.push(v);
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char ch;
cin>>ch;
if(ch=='.')mapp[i][j]=0;
else mapp[i][j]=1;
}
}
BFS(1,1);
cout<<maxx;
return 0;
}
思路:
创造新数列表示相邻两数之差
dp表示当前区段的gcd,dp!=1就更新maxx
代码;
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL a[2010],b[2010];
LL dp[2010][2010];
LL n;
int main(){
LL n;
cin>>n;
for(LL i=1;i<=n;i++)cin>>a[i];
if(n<=1){
cout<<n<<endl;
return 0;
}
for(LL i=2;i<=n;i++)b[i-1]=abs(a[i]-a[i-1]);
n--;
LL maxx=0;
for(LL i=1;i<=n;i++){
for(LL j=1;j+i-1<=n;j++){
if(i==1){
dp[j][j+i-1]=b[j];
}
else{
dp[j][j+i-1]=__gcd(dp[j][j+i-2],b[j+i-1]);
}
if(dp[j][j+i-1]!=1){
maxx=max(maxx,i);
}
}
}
cout<<maxx+1;
return 0;
}
思路:质因数+筛数
代码:
#include<bits/stdc++.h>
using namespace std;
bool book[1000010];
int shu[1000010];
int EVA[1000010];
int cnt=0;
int n,m;
void add(int x){
if(book[x]==0){
book[x]=1;
EVA[++cnt]=x;
}
}
int ans[1000010],Ans=0;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>shu[i];
for(int i=1;i<=n;i++){
int res=shu[i];
for(int j=2;j<=sqrt(res);j++){
//cout<<j<<endl;
if(res%j==0){
add(j);
while(res%j==0)res/=j;
}
}
if(res!=1)add(res);
}
for(int i=1;i<=cnt;i++){
for(int j=EVA[i];j<=m;j+=EVA[i]){
book[j]=1;
}
}
//cout<<endl;
for(int i=1;i<=m;i++)if(book[i]==0)ans[++Ans]=i;
cout<<Ans<<endl;
for(int i=1;i<=Ans;i++)cout<<ans[i]<<endl;
return 0;
}
思路:
先离散化a,b,求出a相对于b的相对排序,求逆序对。
我猜的,然后对了,这种题果然只能靠直觉、
代码:
#include<bits/stdc++.h>
using namespace std;
int a[100010];
int b[100010];
int t[100010];
int id[100010];
int T[100010];
int n;
const int P=1e8-7;
long long res=0;
void guibing(int l,int r){
if(l>=r)return ;
int mid = (l+r)/2;
guibing(l,mid);guibing(mid+1,r);
int i=l,j=mid+1,k=l;
while(i<=mid && j<=r){
if(a[i]<a[j]){
T[k++] = a[i++];
}
else{
T[k++] = a[j++];
res=(res+(mid-i+1))%P;
}
}
while(i<=mid)T[k++]=a[i++];
while(j<=r)T[k++]=a[j++];
for(i=l;i<=r;i++)a[i]=T[i];
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],t[i]=a[i];
sort(t+1,t+n+1);
for(int i=1;i<=n;i++){
a[i] = lower_bound(t+1,t+n+1,a[i])-t;
}
for(int i=1;i<=n;i++)cin>>b[i],t[i]=b[i];
sort(t+1,t+n+1);
for(int i=1;i<=n;i++){
b[i] = lower_bound(t+1,t+n+1,b[i])-t;
id[b[i]]=i;
}
for(int i=1;i<=n;i++)a[i]=id[a[i]];
//for(int i=1;i<=n;i++)cout<<a[i]<<' ';cout<<endl;
guibing(1,n);
cout<<res;
return 0;
}