1323 条题解
-
0Hory LV 10 @ 2016-09-19 13:02:06
无聊写的A+B问题。。。。
c++
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define INF 0x7fffffff
using namespace std;
int a,b,cnt;
int ans[10],h[10],head[10],father[10],bit[10],mp[3][3],w[3],c[3],f[3];
struct data1{int to,next,v;}e[10];
struct data2{int x,y,v;}ed[10];
struct data3{int l,r,sum;}tr[10];
void insert(int u,int v,int w)
{
cnt++;
e[cnt].to=v;
e[cnt].v=w;
e[cnt].next=head[u];
head[u]=cnt;
}
bool bfs()
{
int q[10],t=0,w=1,i,now;
memset(h,-1,sizeof(h));
q[0]=h[0]=0;
while(t<w)
{
now=q[t];t++;
i=head[now];
while(i)
{
if(e[i].v&&h[e[i].to]==-1)
{q[w++]=e[i].to;h[e[i].to]=h[now]+1;}
i=e[i].next;
}
}
if(h[3]==-1)return 0;
return 1;
}
int dfs(int x,int f)
{
if(x==3)return f;
int w,used=0,i=head[x];
while(i)
{
if(e[i].v&&h[e[i].to]==h[x]+1)
{
w=f-used;
w=dfs(e[i].to,min(e[i].v,w));
e[i].v-=w;
e[i^1].v+=w;
used+=w;
if(used==f)return f;
}
i=e[i].next;
}
if(!used)h[x]=-1;
return used;
}
void dinic()
{
cnt=1;
memset(head,0,sizeof(head));
insert(0,1,a);insert(1,0,0);
insert(1,3,INF);insert(3,1,0);
insert(0,2,b);insert(2,0,0);
insert(2,3,INF);insert(3,2,0);
while(bfs()){ans[1]+=dfs(0,INF);}
}
void spfa()
{
cnt=0;
memset(head,0,sizeof(head));
insert(0,1,a);
insert(1,2,b);
int dis[3],q[10],t=0,w=1,i,now;
bool inq[10];
memset(dis,127/3,sizeof(dis));
memset(inq,0,sizeof(inq));
q[0]=dis[0]=0;inq[0]=1;
while(t<w)
{
now=q[t];t++;
i=head[now];
while(i)
{
if(e[i].v+dis[now]<dis[e[i].to])
{
dis[e[i].to]=e[i].v+dis[now];
if(!inq[e[i].to])
{
inq[e[i].to]=1;
q[w++]=e[i].to;
}
}
i=e[i].next;
}
inq[now]=0;
}
ans[2]=dis[2];
}
void floyd()
{
memset(mp,127/3,sizeof(mp));
mp[1][2]=a;mp[2][3]=b;
for(int k=1;k<=3;k++)
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
ans[3]=mp[1][3];
}
void seg_build(int k,int s,int t)
{
tr[k].l=s;tr[k].r=t;
if(s==t)return;
int mid=(s+t)>>1;
seg_build(k<<1,s,mid);
seg_build(k<<1|1,mid+1,t);
tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}
void seg_change(int k,int x,int y)
{
int l=tr[k].l,r=tr[k].r;
tr[k].sum+=y;
if(l==r)return;
int mid=(l+r)>>1;
if(y<=mid)seg_change(k<<1,x,y);
else seg_change(k<<1|1,x,y);
}
int seg_ask(int k,int x,int y)
{
int l=tr[k].l,r=tr[k].r;
if(x==l&&y==r)return tr[k].sum;
int mid=(l+r)>>1;
if(mid>=y)return seg_ask(k<<1,x,y);
else if(mid<x)return seg_ask(k<<1|1,x,y);
else return seg_ask(k<<1,x,mid)+seg_ask(k<<1|1,mid+1,y);
}
void segtree()
{
seg_build(1,1,2);
seg_change(1,1,a);
seg_change(1,1,b);
ans[4]=seg_ask(1,1,2);
}
int lowbit(int x){return x&(-x);}
int bit_ask(int x)
{
int s=0;
while(x)
{
s+=bit[x];
x-=lowbit(x);
}
return s;
}
void bit_change(int x,int y)
{
while(x<=2)
{
bit[x]+=y;
x+=lowbit(x);
}
}
void bitree()
{
bit_change(1,a);
bit_change(2,b);
ans[5]=bit_ask(2);
}
int find(int x){return x==find(x)?x:father[x]=find(father[x]);}
bool cmp(data2 a,data2 b){return a.v<b.v;}
void kruskal()
{
ed[1].x=0;ed[1].y=1;ed[1].v=a;
ed[2].x=1;ed[2].y=2;ed[2].v=b;
sort(ed+1,ed+3,cmp);
for(int i=0;i<=2;i++)father[i]=i;
for(int i=1;i<=2;i++)
{
int x=father[ed[i].x],y=father[ed[i].y];
if(x!=y){father[x]=y;ans[6]+=ed[i].v;}
}
}
void dp()
{
w[1]=w[2]=1;
c[1]=a;c[2]=b;
for(int i=1;i<=2;i++)
for(int v=2;v>=w[i];v--)
f[v]=max(f[v],f[v-w[i]]+c[i]);
ans[7]=f[2];
}
int main()
{
scanf("%d%d",&a,&b);
dinic();//cout<<ans[1]<<endl;
spfa();//cout<<ans[2]<<endl;
floyd();//cout<<ans[3]<<endl;
segtree();//cout<<ans[4]<<endl;
bitree();//cout<<ans[5]<<endl;
kruskal();//cout<<ans[6]<<endl;
dp();//cout<<ans[7]<<endl;
for(int i=1;i<=7;i++)ans[0]+=ans[i];
ans[0]/=7;
printf("%d",ans[0]);
return 0;
}
23333333333333333333333333333333333333333333333333333---转自黄学长
-
02016-09-17 11:51:34@
#include<cstdlib> #include<cstdio> int main(int a,int b,int k) { if (k) scanf("%d%d",&a,&b); printf("%d",b==0?a:main(a^b,(a&b)<<1,0)); exit(0); }
233333333333333333
-
02016-09-16 21:03:13@
#include<stdio.h>
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d",a+b);
return 0;
} -
02016-09-14 20:45:22@
#include<bits/stdc++.h>
int main(int a,int b,int s)
{
if (!s)
{
printf("%d",b==0?a:main(a^b,(a&b)<<1,0));
exit(0);
}
scanf("%d%d",&a,&b);
main(a,b,0);
}
23333333333333333 -
02016-09-13 23:27:26@
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
} -
02016-09-06 19:53:33@
#include<stdio.h>
int main( )
{
int a,b,c;
scanf("%d %d",&a,&b);
printf("%d",a+b);
return 0;
} -
02016-09-06 19:50:39@
#include<stdio.h>
int main( )
{
int a,b,c;
scanf("%d %d",&a,&b);
printf("%d",a+b);
return 0;
} -
02016-09-06 19:49:17@
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d",a/b);
return 0;
} -
02016-09-03 15:52:21@
#include <iostream> #define a using #define b namespace #define c std #define d ; #define e int #define f main #define g ( #define h ) #define i { #define j } #define k cin #define l >> #define m return #define n , #define o cout #define p << #define q endl #define r + #define s 0 a b c d e f g h i e aaa n bbb d k l aaa l bbb d o p aaa r bbb p q d m s d j
加强版
-
02016-09-03 15:48:40@
#include <iostream> #define a using #define b namespace #define c std #define d ; #define e int #define f main #define g ( #define h ) #define i { #define j } #define k cin #define l >> #define m return #define n , #define o cout #define p << #define q endl #define r + #define s 0 a b c d e f g h { e aaa n bbb d k l aaa l bbb d o p aaa r bbb p q d m s d }
我居然笑了
-
02016-09-01 21:13:52@
#include<cstdio>
int plus(int a,int b)
{
return b==0?a:plus(a^b,(a&b)<<1);
}
int main()
{
int a,b;scanf("%d%d",&a,&b);
printf("%d",plus(a,b));
}
位运算 -
02016-09-01 21:00:55@
#include<cstdio>
int plus(int a,int b)
{
if (b==0) return a;
plus(a^b,(a&b)<<1);
}
int main()
{
int a,b;scanf("%d%d",&a,&b);
printf("%d",plus(a,b));
}
23333 -
02016-08-23 14:09:56@
我才是最Diao的
program 呵呵; var a,b:longint; begin randomize; readln(a,b); a:=random(60000); writeln(a); end.
-
02016-08-18 21:50:12@
var a,b:integer;
begin
read(a,b);
write(a+b);
end. -
02016-08-18 13:06:29@
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int a[1001][1001];
int f[1001][1001];
int max(int a, int b){
return a > b ? a : b;
}
int main(){
memset(a, 0, sizeof(a));
memset(f, 0, sizeof(f));
int i,j,k,n,m;
scanf("%d%d", &n, &m);
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++) scanf("%d", &a[i][j]);
for(i = 1; i <= n; i++){
for(j = 1; j <= m; j++){
int tmp = max(f[i - 1][j] + a[i][j], f[i][j - 1] + a[i][j]);
f[i][j] = (f[i][j], tmp);
}
}
printf("%d\n", f[n][m]);
return 0;
} -
02016-08-18 13:05:57@
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int a[100001];
int f[100001];
int max(int a, int b){
return a > b ? a : b;
}
int pre[100001];
int main(){
memset(pre, 0, sizeof(pre));
int i,j,k,n,m;
int ans = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++) scanf("%d", &a[i]);
f[0] = 0; f[1] = 1; pre[1] = 1;
for(i = 2; i <= n; i++){
int MAXN = 0;
ans = 0;
for(j = 1; j <= i; j++){
if(a[j] <= a[i] && f[j] > MAXN){MAXN = f[j]; ans = j;}
}
f[i] = max(MAXN + 1, 1);
if(ans == 0) pre[i] = i;
else pre[i] = ans;
}
printf("%d\n", f[n]);
int MAXN = 0;
for(i = n; i >= 1; i--){
if(f[i] > MAXN){
MAXN = f[i];
ans = i;
}
}
printf("%d ", a[ans]);
for(i = ans; i != pre[i]; i = pre[i]){
printf("%d ", a[pre[i]]);
}
//for(i = 1; i <= n; i++) cout<<i<<": "<<pre[i]<<endl;
printf("\n");
return 0;
}
/*
6
5 3 4 8 6 7
*/ -
02016-08-18 13:05:21@
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 1e16
using namespace std;
long long f[10001];
int a[10001];
int main(){
memset(f, 0, sizeof(f));
freopen("rand.out", "r", stdin);
freopen("硬币.out", "w", stdout);
int i,j,k,n,m,x,y,z;
scanf("%d", &m);
for(i = 1; i <=m; i++) scanf("%d", &a[i]);
scanf("%d", &n);
f[0] = 0;
for(i = 1; i <= n; i++){
long long minn = MAXN;
for(j = 1; j <= m && a[j] <= i; j++){
//cout<<i<<": "<<f[i - a[j]]<<endl;
if(f[i - a[j]] < minn && a[j] <= i){
minn = f[i - a[j]] + 1;
//cout<<i<<": "<<1<<endl;
}
}
f[i] = minn;
//else if(flag == 0)
//cout<<i<<": "<<f[i]<<endl;
}
if(f[n] == MAXN) printf("Impossible\n");
else printf("%lld\n", f[n]);
return 0;
} -
02016-08-14 12:04:16@
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
}
你们为什么这么diao -
02016-04-20 22:03:34@
#include <stdio.h> int main() { int a, b; scanf("%d%d", &a, &b); printf("%d\n", a + b); return 0; }
-
02016-04-17 20:37:03@
A+B问题,一道历史悠久的问题,下面就给出该题的多种解法。
解法一:首先用cin/scanf读入两个int类型数a和b,再用cout/printf输出a+b的结果。核心代码:cin>>a>>b; cout<<a+b<<endl;
解法二:为了防止计算a+b时占用过多内存(其实也没多少),可使用C++的提供的内联汇编来完成本题。核心代码:
__asm
(
"mov %1,%%eax \n\t"
"mov %2,%%ebx \n\t"
"add %%eax,%%ebx \n\t"
"mov %%ebx,%0"
:"=m"(b)
:"m"(a),"m"(b)
);
最后输出b即可。(事实上,这就是不让编译器替我们把高级语言翻译为汇编语言,直接就替它做了)解法三:为防止出题人故意出大数据恶心我们,可采用高精度加法完成此题。核心代码:
char s1[101] , s2[101] , l1 , l2 , l3;
int a[101]={0} , b[101]={0} , c[102]={0};
cin >> s1 >> s2;
l1 = strlen(s1);
l2 = strlen(s2);
for (int i = 0 ; i < l1 ; i++) a[l1 - i] = s1[i] - '0';
for (int i = 0 ; i < l2 ; i++) b[l2 - i] = s2[i] - '0';
l3 = max(l1 , l2);
for (int i = 1 ; i <= l3 ; i++)
{
c[i] += a[i] + b[i];
c[i + 1] = c[i] / 10;
c[i] %= 10;
}
if (c[l3 + 1] == 1) l3++;
for (int i = l3 ; i >= 1 ; i--) cout << c[i]; cout<<endl;解法四:(从此以后均为扯淡)
设f[i][j]表示i+j的值。可得状态转移方程为f[i][j] = max(f[i-1][j]+1 , f[i][j-1]+1)。由于任何数加0数值不变(公理),所以边界条件为f[0][i]=f[i][0]=i。最后输出f[a][b]即可。核心代码:
for (int i = 1 ; i <= a ; i++) f[i][0] = i;
for (int i = 1 ; i <= b ; i++) f[0][i] = i;
for (int i = 1 ; i <= a ; i++)
for (int j = 1 ; j <= b ; j++)
f[i][j] = max(f[i - 1][j] + 1 , f[i][j - 1] + 1);解法五:分析解法四时间复杂度知若时间为O(a*b)。若a*b过大则程序必定超时。观察代码容易发现,根据分配律(公理)得:
f[i][j-1]+1=(i)+(j-1)+1=(i)+(j)-1+1=(i-1)+(j)+1=f[i-1][j]+1
根据上式我们可以立即将时间和空间复杂度都降低一维。核心代码:
f[0] = 0;
for (int i = 1 ; i <= max(a , b) ; i++)
f[i] = f[i - 1] + 1;最后输出f[a]+f[b]。可以看到,时间复杂度为O(max(a,b)),为线性,可以通过此题。
解法六:
首先orz给出本解法的神犇
设图G=(V, E)为一流网络,包含2个源点S1、S2和1个汇点T。构造该图使得:
从S1、S2分别发出a、b条边,每条边的容量为1,这些边应当连接到max(a, b)个顶点上,且没有两条边拥有相同的两个端点。从这max(a, b)个顶点分别连一条容量为+∞的边到汇点T。然后在这个流网络上面跑Dinic算法、Ford-Fulkerson算法之类的东西,可以练练写网络流的手感。
信息
- ID
- 1000
- 难度
- 9
- 分类
- (无)
- 标签
- (无)
- 递交数
- 74446
- 已通过
- 28492
- 通过率
- 38%
- 被复制
- 223