思路:可以二分枚举最久的时间,然后判断一下每个炸弹的爆炸时间和这个最久的时间相差多少,如果炸弹爆炸的时间比枚举的时间大,就不用管,因为它到最后肯定也不会爆炸,如果比枚举的时间小的话,就要记录它们的差值。最后对所有炸弹累加的差值和枚举的最久时间进行判断(枚举的最久时间其实就是能进行多少轮,也就是能给炸弹加多少次1)。
注意会爆int。
————————————————
版权声明:本文为CSDN博主「Than-」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44137005/article/details/109447322
#include
using namespace std;
#define jiechufengyin std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
const int maxn = 1e5 + 10;
const int minn = 1e3 + 10;
#define mod 1000000007
int gcd(int a, int b){
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b){
return a * b / gcd(a, b);
}
ll ksm(ll a, ll b){
ll ans = 1;
while (b){
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int a[maxn];
int Case = 1, t, n;
int check(ll x){
ll sum = 0;
for (int i = 1; i <= n; i++){
if (a[i] < x)
sum += (x - a[i]);
}
if (sum <= x)
return 1;
else
return 0;
}
int main(){
jiechufengyin;
cin >> t;
while (t--){
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
ll l = 0, r = 2*(ll)inf;
while (l <= r){
ll mid = (l + r) / 2;
if (check(mid))
l = mid + 1;
else
r = mid - 1;
}
printf("Case #%d: %lld\n", Case++, l);
}
return 0;
}
最大生成树,最小生成树选最小的边换成选最大的边即可。
但是注意题目是说用位运算&实现。
#include
using namespace std;
typedef long ll;
const int N = 3e5 + 7;
const int M = 3e5 + 7;
#define jiechufengyin std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
struct point{
int x;
int y;
int w;
}p[M];
int t;
int n,m, ans;
int f[N];
void init(){
for(int i = 0; i < n + 1; i++){
f[i] = i;
}
}
int findfather(int v){
if(f[v] == v) return v;
else{
f[v] = findfather(f[v]);
return f[v];
}
}
bool cmp(point a, point b){
return a.w > b.w;
}
bool flag;
int num;
void merge(int u, int v, int x){
int t1, t2;
t1 = findfather(u);
t2 = findfather(v);
if(t1 != t2){
if(flag){
ans = p[x].w;
flag = 0;
}
else{
ans &= p[x].w;
}
f[t1]=t2;
num--;
}
return ;
}
int main(){
jiechufengyin;
cin >> t;
while(t--){
cin >> n >> m;
init();
for(int i = 0; i < m; i++){
cin >> p[i].x >> p[i].y >> p[i].w;
}
sort(p, p + m, cmp);
flag = 1;
num = n-1;
for(int i = 0; i < m; i++){
merge(p[i].x, p[i].y, i);
}
if(num!=0){
cout << '0' << endl;
}
else{
cout << ans << endl;
}
}
return 0;
}
队友A的
#include
#include
#include
#include
using namespace std;
using namespace std;
typedef long long ll;
int dp[105][105];
struct yyqxwyb
{
int x,y;
int v;
};
yyqxwyb zz[10050];
int a[105][105];
bool cmp(yyqxwyb a1,yyqxwyb a2)
{
if(a1.v!=a2.v)return a1.v<a2.v;
else if(a1.x!=a2.x)return a1.x<a2.x;
else return a1.y<a2.y;
}
int main()
{
int r,c;
cin>>r>>c;
int f=1;
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
cin>>a[i][j];
zz[f].v=a[i][j];
zz[f].x=i;
zz[f].y=j;
f++;
}
}
sort(zz+1,zz+f,cmp);
//dp[zz[1].x][zz[1].y]=1;
for(int i=1;i<=r*c;i++)
{
int bz=a[zz[i].x][zz[i].y];
if(a[zz[i].x+1][zz[i].y]<bz)dp[zz[i].x][zz[i].y]=max(dp[zz[i].x][zz[i].y],dp[zz[i].x+1][zz[i].y]);
if(a[zz[i].x-1][zz[i].y]<bz)dp[zz[i].x][zz[i].y]=max(dp[zz[i].x][zz[i].y],dp[zz[i].x-1][zz[i].y]);
if(a[zz[i].x][zz[i].y+1]<bz)dp[zz[i].x][zz[i].y]=max(dp[zz[i].x][zz[i].y],dp[zz[i].x][zz[i].y+1]);
if(a[zz[i].x][zz[i].y-1]<bz)dp[zz[i].x][zz[i].y]=max(dp[zz[i].x][zz[i].y],dp[zz[i].x][zz[i].y-1]);
dp[zz[i].x][zz[i].y]+=1;
}
int maxx=0;
for(int i=1;i<=r*c;i++)
{
maxx=max(maxx,dp[zz[i].x][zz[i].y]);
}
cout<<maxx<<'\n';
return 0;
}
水题,但是要注意都要开ll,会爆int
#include
using namespace std;
typedef long long ll;
ll t;
ll n, ans;
ll qiuweishu(ll x)
{
ll sum = 0;
while(x)
{
sum += (x % 10);
x /= 10;
}
return sum;
}
int main()
{
cin >> t;
for (int i = 1; i <= t; ++i)
{
cin >> n;
ans = 0;
ll temp;
if(n % 2050 == 0){
temp = n / 2050;
ans = qiuweishu(temp);
cout << ans << endl;
continue;
}
else cout << "-1" << endl;
}
return 0;
}