T1
A sequence a0, a1, …, at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t.
You are given a sequence b0, b1, …, bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing?
暴力贪心,将每一个比前面小(或等于)的加到比前一个大即可。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,d,i,b[2010];
long long int ans=0;
scanf("%d%d",&n,&d);
for(i=0;i<=n-1;i++)
scanf("%d",&b[i]);
for(i=1;i<=n-1;i++)
{if(b[i]<=b[i-1])
{if(b[i]==b[i-1])
{ans++;b[i]+=d;
}
else
{ans+=((b[i-1]-b[i])/d+1);
b[i]+=((b[i-1]-b[i])/d+1)*d;
}
}
}
cout<<ans;
}
T2
Jack is working on his jumping skills recently. Currently he’s located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he’ll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.
现将输入取绝对值,之后其实只要一直加到值为所求或大于并且为双数即可输出(往后跳可以调一切双数。)
#include<bits/stdc++.h>
using namespace std;
int main() {
int x,a=0,c=0;
scanf("%d",&x);
if(x<0)x=-x;
for(int i=1; i<=100000&&!(a>x&&!((a-x)&1))&&a!=x; i++) {
a+=i;
c++;
}
!x?cout<<0:cout<<c;
}
T4
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges.
状态压缩dp。第一维压路径,第二维表示路径终点,枚举所有路径与其他路径产生环的个数,最后除以2输出。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[1<<19][19];int x,y,n,m;bool flag,mp[19][19];
inline ll solve()
{ll ans=0;int i,j,k;
for(i=0;i<(1<<n);i++)
{flag=1;
for(j=0;j<n;j++)
if(((i&(1<<j)))&&(!dp[i][j]))
if(flag)flag=0,k=j;
else
{for(y=k+1;y<n;y++)
if(((i&(1<<y)))&&(mp[y][j]))
dp[i][j]+=dp[i-(1<<j)][y];
if(mp[k][j])
ans+=dp[i][j];}
}
return ans>>=1;
}
int main()
{ scanf("%d%d",&n,&m);
while(m--)
{ scanf("%d%d",&x,&y);
x--,y--;
mp[x][y]=mp[y][x]=1;
if(x>y)swap(x,y);
dp[(1<<x)+(1<<y)][y]++;}
printf("%lld",solve());}