T1 popust (greedy
TimeLimit: 1000MS
Memory Limit: 32768KB
Mirko hungry as a bear, he stumbled across a local restaurant. The restaurant offers \ (n \) seed meal, there is an interesting pricing policy: each meal with two specified price, \ (A_i \) and \ (B_i \) . Mirko meal can only be used to pay the first price \ (A \) , the latter can only pay \ (B \) price. He is known a meal can only be set once, but several do not know what to order, so he wanted to know from \ (1 \) species to \ (N \) for the least cost species (in fact, he did not care in the end what to eat species). Please help him think about it.
Input :
The first line of input contains a positive integer (N (2≤N≤500000) \) \ , the number of types of meals provided by the restaurant.
The following \ (N \) line contains two positive integers, \ (A_i \) and \ (B_i \) \ ((. 1 ≤ Ai, of Bi ≤ 000. 1 000 000) \) , represents the \ (I \) seed meal the two prices.
Output : \
(N \) line, the \ (i \) line represents exactly set \ (i \) Lowest price seed meal
input
3
10 5
9 3
10 5
output
9
13
18
input
2
100 1
1 100
output
1
2
input
5
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
output
1000000000
2000000000
3000000000
4000000000
5000000000
Meaning that the election \ (x \) when the kinds of goods, which pay a \ (A_i \) , the rest are paid \ (B_i \) ;
Since \ (A_i \) is only one, we considered separately;
If you just take \ (B \) , we must take from small to large;
First sort;
We took the first small to large \ (x \) a \ (B \) , which now want a change \ (A \) ;
There are two methods to change;
- Before the election \ (X \) a \ (B \) , the one corresponding to \ (A \) , this article \ (A_i-B_i \) , the former should be \ (X \) a smallest;
- Which have not been selected in a selected minimum \ (A \) , the maximum \ (B \) changed;
Second, we can pre-out \ (Mina [] \) represents the \ (i + 1 \ sim n \) smallest \ (A \) ;
The first can be a variable in solving the adoptive transfer;
Time complexity is \ (O (nlogn) \) ;
\(Code\)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=500005;
int n,pos;
struct food
{
int x,y;
bool operator<(food w)const
{
return y<w.y;
}
}a[N];
ll minx[N],minc;
ll ans;
inline int read()
{
int x=0,f=1;char st=getchar();
while(st<'0'||st>'9'){if(st=='-') f=-1;st=getchar();}
while(st>='0'&&st<='9') x=x*10+st-'0',st=getchar();
return x*f;
}
int main()
{
freopen("popust.in","r",stdin);
freopen("popust.out","w",stdout);
n=read();
for(int i=1;i<=n;i++)
{
a[i].x=read(),a[i].y=read();
}
sort(a+1,a+1+n);
minx[n]=9999999999999;
for(int i=n-1;i>=1;i--)
minx[i]=min(minx[i+1],(ll)a[i+1].x);
minc=9999999999999;
for(int i=1;i<=n;i++)
{
minc=min(minc,(ll)a[i].x-a[i].y);
ans+=a[i].y;
printf("%lld\n",min(ans-a[i].y+minx[i],ans+minc));
}
fclose(stdin);
fclose(stdout);
return 0;
}
T2 informacije (FIG bipartite
TimeLimit: 1000MS
Memory Limit: 32768KB
A sequence \ (A \) , inclusion \ (1 \) to \ (N \) co \ (N \) elements (each element appears once and only once) to give \ (M \) th About \ (a \) is described. Description format is as follows:
$. 1 X Y V $ represents the position of the \ ([x, y] \ ) the maximum value is equal to between \ (V \)
\ (2 \ X \ Y \ V \) represents the position of the \ ([x, y ] \) minimum is equal to a value between \ (V \)
Please outputs satisfy the above \ (M \) sequences described herein. You may have different options, either the output.
Output no solution \ (- 1 \) ;
\(N (1 ≤ N ≤ 200)\)\(M (0 ≤ M ≤ 40 000)\)
It is seeking legal arrangement;
What number is seeking a position can be put;
One side is the location, one side is the number one location can even choose the number one side;
This is a bipartite graph;
This position number associated with the representative position is selected from this number;
This is the maximum matching of the bipartite graph;
If a match is not perfect, it is configured not aligned;
Outputs \ (- 1 \) ;
Pretreatment can put the number of each location, each location number can be put, if sexual gratification, even an edge;
Time complexity is \ (O (n ^ 3) \)
\(Code\)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=205;
int n,m,opt,l,r,v,ans;
bool pd[N][N];
int l1[N],l2[N];//上界,与下界
struct skr
{
int to,nxt;
}a[N*N*2];
int head[N<<1],cnt;
int match[N<<1],vis[N<<1];
inline void add(int x,int y)
{
a[++cnt].to=y;a[cnt].nxt=head[x];head[x]=cnt;
}
inline int read()
{
int x=0,f=1;char st=getchar();
while(st<'0'||st>'9'){if(st=='-') f=-1;st=getchar();}
while(st>='0'&&st<='9') x=x*10+st-'0',st=getchar();
return x*f;
}
inline bool dfs(int x)
{
for(int i=head[x];i;i=a[i].nxt)
{
int y=a[i].to;
if(!vis[y])
{
vis[y]=1;
if(!match[y]||dfs(match[y]))
{
match[y]=x;
return 1;
}
}
}
return 0;
}
int main()
{
freopen("informacije.in","r",stdin);
freopen("informacije.out","w",stdout);
n=read();m=read();
for(int i=1;i<=n;i++)
l1[i]=n,l2[i]=1;
while(m--)
{
opt=read();l=read();r=read();v=read();
for(int i=1;i<l;i++)
pd[v][i]=1;
for(int i=r+1;i<=n;i++)
pd[v][i]=1;
if(opt==1)
{
for(int i=l;i<=r;i++)
l1[i]=min(l1[i],v);
}
else
{
for(int i=l;i<=r;i++)
l2[i]=max(l2[i],v);
}
}
for(int i=1;i<=n;i++)
{
for(int j=l2[i];j<=l1[i];j++)
if(!pd[j][i])
{
add(j,i+n);
add(i+n,j);
}
}
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof vis);
if(dfs(i)) ans++;
}
if(ans<n)
{
printf("-1");
fclose(stdin);
fclose(stdout);
return 0;
}
for(int i=1;i<=n;i++)
printf("%d ",match[i+n]);
fclose(stdin);
fclose(stdout);
return 0;
}