开学啦!
贪心策略:先安排只能用左右手的人,再安排剩下的人和全能的人再一起,最后考虑剩下的全能人。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int main() {
int l,r,a,ans;
cin >> l >> r >> a;
ans=min(l,r)*2;
r-=ans/2;l-=ans/2;
if (l>0) {
int q=min(l,a);
ans+=q*2;
a-=q;l-=q;
} else {
int q=min(r,a);
ans+=q*2;
a-=q;r-=q;
}
if (a>0) {
ans+=a/2*2;
}
cout << ans;
return 0;
}
Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.
Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.
Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.
You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.
The first line contains two integers n, m (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message.
The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message.
It is guaranteed that x1 + ... + xn = y1 + ... + ym. Also, it is guaranteed that x1 + ... + xn ≤ 106.
Print the maximum number of files the intercepted array could consist of.
7 6 2 5 3 1 11 4 4 7 8 2 4 1 8
3
3 3 1 10 100 1 100 10
2
1 4 4 1 1 1 1
1
In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.
In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.
In the third example the only possibility is that the archive contains a single file of size 4.
贪心,按顺序用two-pointer在两组分组里扫,尽量让当前分组的总和相等。
话说分组这个词前几天计算机网络课上刚刚听过= =
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
ll a[maxn],b[maxn];
int main() {
int n,m,i,j;
ll sa,sb,sq;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) {
scanf("%I64d",&a[i]);
}
int ans=0;
sb=0;
sa=a[1];sq=1;
for (i=1;i<=m;i++) {
scanf("%I64d",&b[i]);
sb+=b[i];
while (sa<sb&&sq<n) {
sq++;
sa+=a[sq];
}
if (sa==sb) {
ans++;
sb=0;sq++;sa=a[sq];
}
}
printf("%d\n",ans);
return 0;
}
规律很显然了,偶数项对应的大小关系和n/2之后的大小关系相同。
那么我们只要不断将数列长度折半,直到要查询的下标为奇数就可以了。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int main() {
ll n,i,ans,q,x;
scanf("%I64d%I64d",&n,&q);
for (i=1;i<=q;i++) {
scanf("%I64d",&x);
ll nq=n;
ans=0;
while (nq>0) {
if (x%2==1) {
ans+=(x+1)/2;
break;
}
ans+=(nq+1)/2;
if (nq%2==0) {
x/=2;
} else {
if (x==2) {
x=nq/2;
} else {
x=x/2-1;
}
}
nq/=2;
}
printf("%I64d\n",ans);
}
return 0;
}
BigData Inc. is a corporation that has n data centers indexed from 1 to n that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!).
Main feature of services offered by BigData Inc. is the access availability guarantee even under the circumstances of any data center having an outage. Such a guarantee is ensured by using the two-way replication. Two-way replication is such an approach for data storage that any piece of data is represented by two identical copies that are stored in two different data centers.
For each of m company clients, let us denote indices of two different data centers storing this client data as ci, 1 and ci, 2.
In order to keep data centers operational and safe, the software running on data center computers is being updated regularly. Release cycle of BigData Inc. is one day meaning that the new version of software is being deployed to the data center computers each day.
Data center software update is a non-trivial long process, that is why there is a special hour-long time frame that is dedicated for data center maintenance. During the maintenance period, data center computers are installing software updates, and thus they may be unavailable. Consider the day to be exactly h hours long. For each data center there is an integer uj (0 ≤ uj ≤ h - 1) defining the index of an hour of day, such that during this hour data center j is unavailable due to maintenance.
Summing up everything above, the condition uci, 1 ≠ uci, 2 should hold for each client, or otherwise his data may be unaccessible while data centers that store it are under maintenance.
Due to occasional timezone change in different cities all over the world, the maintenance time in some of the data centers may change by one hour sometimes. Company should be prepared for such situation, that is why they decided to conduct an experiment, choosing some non-empty subset of data centers, and shifting the maintenance time for them by an hour later (i.e. if uj = h - 1, then the new maintenance hour would become 0, otherwise it would become uj + 1). Nonetheless, such an experiment should not break the accessibility guarantees, meaning that data of any client should be still available during any hour of a day after the data center maintenance times are changed.
Such an experiment would provide useful insights, but changing update time is quite an expensive procedure, that is why the company asked you to find out the minimum number of data centers that have to be included in an experiment in order to keep the data accessibility guarantees.
The first line of input contains three integers n, m and h (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000, 2 ≤ h ≤ 100 000), the number of company data centers, number of clients and the day length of day measured in hours.
The second line of input contains n integers u1, u2, ..., un (0 ≤ uj < h), j-th of these numbers is an index of a maintenance hour for data center j.
Each of the next m lines contains two integers ci, 1 and ci, 2 (1 ≤ ci, 1, ci, 2 ≤ n, ci, 1 ≠ ci, 2), defining the data center indices containing the data of client i.
It is guaranteed that the given maintenance schedule allows each client to access at least one copy of his data at any moment of day.
In the first line print the minimum possible number of data centers k (1 ≤ k ≤ n) that have to be included in an experiment in order to keep the data available for any client.
In the second line print k distinct integers x1, x2, ..., xk (1 ≤ xi ≤ n), the indices of data centers whose maintenance time will be shifted by one hour later. Data center indices may be printed in any order.
If there are several possible answers, it is allowed to print any of them. It is guaranteed that at there is at least one valid choice of data centers.
3 3 5 4 4 0 1 3 3 2 3 1
1 3
4 5 4 2 1 0 3 4 3 3 2 1 2 1 4 1 3
4 1 2 3 4
Consider the first sample test. The given answer is the only way to conduct an experiment involving the only data center. In such a scenario the third data center has a maintenance during the hour 1, and no two data centers storing the information of the same client have maintenance at the same hour.
On the other hand, for example, if we shift the maintenance time on hour later for the first data center, then the data of clients 1 and 3 will be unavailable during the hour 0.
某土豪公司建立了n个数据中心,把m份资料每份在其中的两个数据中心备份。
每个数据中心在一天h个小时当中有一个小时需要维护,此时不提供资料下载服务。
现在土豪公司想要将其中若干个数据中心的维护时间向后推迟一小时,并要求一天中任意时刻每份资料都可以被下载,问最少选取多少个数据中心维护。
可以将题目抽象成一个图论模型。
每个数据中心是一个点,若选择A中心调整维护时间时,必须选择B中心,则用一条有向边连接A和B。
按照题目的要求,如果备份同一份数据两个中心的维护时间相差一小时,则两者之间需要连接一条边。
显然建图之后,同一个强连通分量之内的边必须一起选。可以依此缩点构成DAG。而且,要选拓扑序在先的块,则必须选它所指向的所有块。题目要求选取的点最少,显然在DAG内选择出度为0的块是最优的。
那么建图之后tarjan缩点建新图统计度数,最后直接取出度为0的、点数最少的块(强连通分量)就可以了。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int head[maxn],a[maxn],d[maxn];
int dfn[maxn],low[maxn],color[maxn],val[maxn];
bool inst[maxn];
vector<int> v[maxn];
stack<int> st;
int num=0,cnum=0;
struct Edge {
int from,to,pre;
};
Edge edge[maxn*2];
void addedge(int from,int to) {
edge[num]=(Edge){from,to,head[from]};
head[from]=num++;
}
void tarjan(int now) {
num++;
dfn[now]=low[now]=num;
inst[now]=1;
st.push(now);
for (int i=head[now];i!=-1;i=edge[i].pre) {
int to=edge[i].to;
if (!dfn[to]) {
tarjan(to);
low[now]=min(low[now],low[to]);
}
else if (inst[to])
low[now]=min(low[now],dfn[to]);
}
if (dfn[now]==low[now]) {
inst[now]=0;
color[now]=++cnum;
v[cnum].push_back(now);
while (st.top()!=now) {
color[st.top()]=cnum;
inst[st.top()]=0;
v[cnum].push_back(st.top());
st.pop();
}
st.pop();
}
}
int main() {
memset(head,-1,sizeof(head));
num=0;
int n,m,k,i,j,x,y;
scanf("%d%d%d",&n,&m,&k);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
for (i=1;i<=m;i++) {
scanf("%d%d",&x,&y);
if ((a[x]+1)%k==a[y]) addedge(x,y);
if ((a[y]+1)%k==a[x]) addedge(y,x);
}
m=num;num=0;
mem0(dfn);mem0(low);mem0(color);mem0(inst);
for (i=1;i<=n;i++)
if (!dfn[i]) tarjan(i);
int ans=-1;
mem0(d);
for (i=0;i<m;i++)
if (color[edge[i].from]!=color[edge[i].to])
d[color[edge[i].from]]++;
for (i=1;i<=cnum;i++)
if (d[i]==0)
if (ans==-1) ans=i; else
if (v[i].size()<v[ans].size()) ans=i;
printf("%d\n",v[ans].size());
for (i=0;i<v[ans].size();i++) printf("%d ",v[ans][i]);
return 0;
}