测:100
秒过,离散化不解释
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
ll n;
ll a[100005],b[100005];
ll e[100005],ans;
ll k;
struct node{
ll x;
ll id;
}p[100005];
ll read(){
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x*f;
}
ll cmp(const node n1,const node n2){
return n1.xint main()
{
ll i;
n=read();
for(i=1;i<=n;++i){
a[i]=read();b[i]=read();
p[i].x=b[i];p[i].id=i;
}
sort(p+1,p+1+n,cmp);
for(i=1;i<=n;++i){
if(p[i].x!=p[i-1].x){
e[++k]=a[p[i].id];
}
else e[k]+=a[p[i].id];
}
for(i=1;i<=k;++i){
e[i]=e[i]*e[i];
}
for(i=1;i<=k;++i){
ans+=e[i];
}
printf("%lld\n",ans);
return 0;
}
测:0 -> 30 -> 70
Emmmmmm... windows.h QAQ printf() QAQ..
这题用重载运算符,模拟多项式运算即可
#include
#include
#include
#include
#include
#include
using namespace std;
string s;
int n;
struct node{
void check(node &q);
int size;
int a[510];
node operator + (const node &x){
size=size>x.size?size:x.size;
for(int i=0;i<=size;++i)a[i]+=x.a[i];
return *this;
}
node operator - (const node &x){
size=size>x.size?size:x.size;
for(int i=0;i<=size;++i)a[i]=a[i]-x.a[i];
if(a[size]==0)size--;
return *this;
}
node operator * (const node &x){
node y;
memset(y.a,0,sizeof(y.a));
y.size=size+x.size;
for(int i=0;i<=size;++i)
for(int j=0;j<=x.size;++j)y.a[i+j]+=a[i]*x.a[j];
return y;
}
};
stack st;
stack <char> ch;
void check(node &q){
for(int i=0;i<=q.size;++i){
while(q.a[i]<0)q.a[i]+=10007;
q.a[i]=q.a[i]%10007;
}
}
void work(){
node x=st.top();st.pop();
node y=st.top();st.pop();
if(ch.top()=='+'){
x=y+x;
st.push(x);
}
else if(ch.top()=='-'){
x=y-x;
st.push(x);
}
else if(ch.top()=='*'){
x=x*y;
st.push(x);
}
check(st.top());
ch.pop();
}
int pd(char c){
if((c=='+'||c=='-')&&ch.top()!='(')return 1;
if(c=='*'&&ch.top()=='*')return 1;
return 0;
}
int main(){
freopen("simplify.in","r",stdin);
freopen("simplify.out","w",stdout);
int i,j;
cin>>s;
s="(0+"+s+"+0)";
n=s.size();
node x;
memset(x.a,0,sizeof(x.a));
x.size=1;
x.a[1]=1;
//cout<
int p=0;
while(pwhile (s[p]=='('){
ch.push('(');
++p;
}
if(s[p]>='0'&&s[p]<='9'){
node y;
y.size=0;
memset(y.a,0,sizeof(y.a));
while(s[p]>='0'&&s[p]<='9')y.a[0]=y.a[0]*10+s[p++]-'0';
st.push(y);
}
do{
if(s[p]==')'){
while(ch.top()!='(')work();
ch.pop();
}
else if(s[p]=='x')st.push(x);
else{
while (pd(s[p])) work();
ch.push(s[p]);
}
++p;
}
while(p1]==')');
}
node ans=st.top();
printf("%d\n",ans.size);
for(i=0;i<=ans.size;++i)printf("%d\n",ans.a[i]);
return 0;
}
测:30
SPFA打错QAQ
两遍SPFA,边存一个反向的
#include
#include
#include
#include
#include
#include
using namespace std;
#define INF 0x7fffffff
#define ll long long
int n,m,Q;
ll d[3005],x[3005];
int q[3005],b[3005],Head[3005],head[3005],t,T;
struct node{
int u,v,w,next;
}e[100005],p[100005];
ll read(){
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x*f;
}
void spfa1(){
ll h=0,tail=1,u,v,w,i;
for(i=1;i<=n;++i)d[i]=INF;
d[1]=0;
q[1]=1;b[1]=1;
while(h!=tail){
++h;if(h>n)h=1;
u=q[h];
b[u]=0;
for(i=Head[u];i;i=p[i].next){
v=p[i].v;w=p[i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!b[v]){
++tail;if(tail>n)tail=1;
q[tail]=v;
b[v]=1;
}
}
}
}
}
void spfa2(){
ll h=0,tail=1,u,v,w,i;
memset(q,0,sizeof(q));
memset(b,0,sizeof(b));
for(i=1;i<=n;++i)x[i]=INF;
x[1]=0;q[1]=1;b[1]=1;
while(h!=tail){
++h;if(h>n)h=1;
u=q[h];
b[u]=0;
for(i=head[u];i;i=e[i].next){
v=e[i].v;w=e[i].w;
if(x[v]>x[u]+w){
x[v]=x[u]+w;
if(!b[v]){
++tail;if(tail>n)tail=1;
q[tail]=v;
b[v]=1;
}
}
}
}
}
int main(){
freopen("production.in","r",stdin);
freopen("production.out","w",stdout);
int i,j;
n=read();m=read();
ll u,v,w;
for(i=1;i<=m;++i){
u=read();v=read();w=read();
++t;
e[t].u=u;e[t].v=v;e[t].w=w;
e[t].next=head[u];
head[u]=t;
++T;
p[T].u=v;p[T].v=u;p[T].w=w;
p[T].next=Head[v];
Head[v]=T;
}
spfa1();
spfa2();
Q=read();
ll ans;
for(i=1;i<=Q;++i){
ans=0;
u=read();v=read();
if(d[u]!=INF)ans+=d[u];
else {printf("-1\n");continue;}
if(x[v]!=INF)ans+=x[v];
else {printf("-1\n");continue;}
printf("%I64d\n",ans);
}
return 0;
}