C.可以用线段树去做,不过因为这个题目维护的区间,左端点是确定的一定是起点,所以只需先预处理一个从起点到i的高度最大值,然后每次维护一个当前的最高值max,对每次来的物体,取Max[wi]和max的较大值。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 5;
LL Max[maxn];
int main(){
int n,m;
while(cin >> n){
Max[0] = 0;
for(int i = 1;i <= n;i++){
LL tem;
cin >> tem;
Max[i] = max(tem,Max[i-1]);
}
cin >> m;
LL last;
for(int i = 0;i < m;i++){
int w;
LL h;
cin >> w >> h;
if(i == 0){
cout << Max[w] << endl;
//Max[w] += h;
last = Max[w] + h;
}
else{
if(Max[w] >= last){
cout << Max[w] << endl;
//Max[w] += h;
last = Max[w] + h;
}
else{
cout << last << endl;
last += h;
}
}
}
}
return 0;
}
D.要预处理出各种x的个数,如果不用map的话,可以对所有的x先排序,然后再统计。容易看出答案就是可重复元素的全排列,这样就还要统计出那些重复元素的个数。可以发现所有重复的元素,只可能是对应的y相同,x也相同,这样重复数最多为2,求这种可重复的全排列数就好算了。然后要注意的是,因为有除法,所以mod运算需要一点技巧,因为最后只会除以2,这样就可以在每次算阶乘的时候,如果乘的是个偶数,且2还没除够,那就先除2。否则直接相乘取模。因为n的范围并不大,所以直接暴力求阶乘也不会超时。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 5;
LL num[2*maxn];
LL cnt,mod;
LL get(LL x){
LL ret = 1;
for(LL i = 1;i <= x;i++){
if(cnt == 0 || i%2 == 1) ret = (ret * i)%mod;
else ret = ret * i;
while(ret%2 == 0 && cnt > 0){
ret /= 2;
cnt--;
}
ret = ret % mod;
}
return ret % mod;
}
int main(){
int n;
while(cin >> n){
cnt = 0;
for(int i = 0;i < n;i++) cin >> num[i];
for(int i = 0;i < n;i++){
cin >> num[i+n];
if(num[i+n] == num[i]) cnt++;
}
cin >> mod;
sort(num,num+2*n);
LL ans = 1;
int last = num[0];
LL countn = 0;
for(int i = 0;i < 2*n;i++){
if(num[i] != last){
ans = (ans * get(countn))%mod;
countn = 1;
last = num[i];
}
else countn++;
}
ans = (ans * get(countn))%mod;
cout << ans << endl;
}
return 0;
}
E.先把所有的点都标记为同一类,然后dfs(注意要对所有的点,因为可能图本身不是连通的),如果遇到出现了矛盾,就把这个点换成另一种颜色,这种策略一定有解,且不会超时,原因如下。
设能够产生矛盾的边的总数为M,产生矛盾时的情况有三种:
1.如果与这点相连的边数为3,且都和该点颜色相同,则取反后,M-3
2.如果与这点相连的边数为3,颜色相同数为2,则取反后,M-1(因为变成另一种颜色后,可能导致那种颜色矛盾)
3.如果与这点相连的边数为2,颜色都相同,取反后,M-2
这样每次找到一个矛盾的点,把它取反,然后接着搜索与它颜色相同的点是否矛盾即可,(因为其他的点不会因为这个改变而产生矛盾)这样复杂度就是O(M)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 300000 + 5;
vector<int> G[maxn];
int ans[maxn];
void dfs(int x){
int cnt = 0;
for(int i = 0;i < G[x].size();i++){
int to = G[x][i];
//printf("%d %d\n",ans[to],ans[x]);
if(ans[to] == ans[x]) cnt++;
}
if(cnt > 1){//cout << 'x';
ans[x] = !ans[x];
for(int i = 0;i < G[x].size();i++){
int to = G[x][i];
if(ans[to] == ans[x]) dfs(to);
}
}
}
int main(){
int n,m;
while(cin >> n >> m){
for(int i = 1;i <= n;i++) G[i].clear();
for(int i = 0;i < m;i++){
int a,b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
memset(ans,0,sizeof(ans));
for(int i = 1;i <= n;i++)
dfs(i);
for(int i = 1;i <= n;i++) cout << ans[i];
cout << endl;
}
return 0;
}