hdu多校(二) 1004 1007 1010
阿新 • • 發佈:2018-07-26
script lan tin 發生 tex sin 有序 limit str
The game is played on a set of positive integers from 1 to n.
In one step, the player can choose a positive integer from the set, and erase all of its divisors from the set. If a divisor doesn‘t exist it will be ignored.
Alice and Bob choose in turn, the one who cannot choose (current set is empty) loses.
Alice goes first, she wanna know whether she can win. Please judge by outputing ‘Yes‘ or ‘No‘.
Input
There might be multiple test cases, no more than 10. You need to read till the end of input.
For each test case, a line containing an integer n. (1≤n≤500)
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l?ai/bi?
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form ‘add l r‘ or ‘query l r‘, representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there‘re no more than 5 test cases.
註意到並不是所有情況下a[i]加上1之後都會使得a[i]/b[i]的結果發生變化,所以我們用minb[b]維護區間最小的b值, query時如果當前區間的minb不為<=0就可以直接使用之前計算的值,否則就遞歸左右兒子將minb的值累加到sum中。 其實就是個線段樹亂搞,當時沒想到,唉太菜了。
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don‘t want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j) which 1≤i<j≤n and ai>aj.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤100000, numbers in the sequence are in [?109,109]. There‘re 10 test cases.
Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
The game is played on a set of positive integers from 1 to n.
In one step, the player can choose a positive integer from the set, and erase all of its divisors from the set. If a divisor doesn‘t exist it will be ignored.
Alice and Bob choose in turn, the one who cannot choose (current set is empty) loses.
Alice goes first, she wanna know whether she can win. Please judge by outputing ‘Yes‘ or ‘No‘.
For each test case, a line containing an integer n. (1≤n≤500)
Output A line for each test case, ‘Yes‘ or ‘No‘.
Sample Input 1
Sample Output Yes 如果先手取1可以獲勝就取1,如果取1之後後手取y可以獲勝,那麽先手取y就好了,留下的局面是一樣的,所以無論如何先手必勝。
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int n; 5 while(cin>>n){ 6 puts("Yes"); 7 } 8 }
Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l?ai/bi?
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form ‘add l r‘ or ‘query l r‘, representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there‘re no more than 5 test cases.
註意到並不是所有情況下a[i]加上1之後都會使得a[i]/b[i]的結果發生變化,所以我們用minb[b]維護區間最小的b值, query時如果當前區間的minb不為<=0就可以直接使用之前計算的值,否則就遞歸左右兒子將minb的值累加到sum中。 其實就是個線段樹亂搞,當時沒想到,唉太菜了。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 const int MAXN=100010; 5 int b[MAXN],n,m,l,r; 6 char s[15]; 7 class ST{ 8 public: 9 #define lc (id<<1) 10 #define rc (id<<1|1) 11 #define mid ((L+R)>>1) 12 13 int minb[MAXN<<2],sum[MAXN<<2],laz[MAXN<<2]; 14 15 void pushup(int id){ 16 sum[id]=sum[lc]+sum[rc]; 17 minb[id]=min(minb[lc],minb[rc]); 18 } 19 void pushdown(int id,int L,int R){ 20 if(laz[id]){ 21 laz[lc]+=laz[id],minb[lc]-=laz[id]; 22 laz[rc]+=laz[id],minb[rc]-=laz[id]; 23 laz[id]=0; 24 } 25 } 26 void build(int id,int L,int R){ 27 sum[id]=laz[id]=0; 28 if(L==R){ 29 scanf("%d",b+L); 30 minb[id]=b[L]; 31 return; 32 } 33 build(lc,L,mid); 34 build(rc,mid+1,R); 35 pushup(id); 36 } 37 void add(int id,int L,int R,int l,int r){ 38 39 if(L>=l&&R<=r){ 40 laz[id]++; 41 minb[id]--; 42 return ; 43 } 44 pushdown(id,L,R); 45 if(l<=mid) add(lc,L,mid,l,r); 46 if(r>mid) add(rc,mid+1,R,l,r); 47 pushup(id); 48 } 49 int query(int id,int L,int R,int l,int r){ 50 if(minb[id]>0&&L>=l&&R<=r){ 51 return sum[id]; 52 } 53 if(L==R){ 54 if(minb[id]<=0){ 55 int d=(-minb[id]+b[L])/b[L]; 56 sum[id]+=d; 57 minb[id]=b[L]-(-minb[id]/*+b[L]-d*b[L]*/)%b[L]; 58 } 59 return sum[id]; 60 } 61 else{ 62 pushdown(id,L,R); 63 int s=0; 64 if(l<=mid) s+=query(lc,L,mid,l,r); 65 if(r>mid) s+=query(rc,mid+1,R,l,r); 66 pushup(id); 67 return s; 68 } 69 } 70 }a; 71 int main(){ 72 while(scanf("%d%d",&n,&m)!=EOF){ 73 a.build(1,1,n); 74 while(m--){ 75 scanf("%s %d%d",s,&l,&r); 76 if(s[0]==‘a‘){ 77 a.add(1,1,n,l,r); 78 } 79 else{ 80 printf("%d\n",a.query(1,1,n,l,r)); 81 } 82 } 83 } 84 return 0; 85 }
Swaps and Inversions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don‘t want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j) which 1≤i<j≤n and ai>aj.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤100000, numbers in the sequence are in [?109,109]. There‘re 10 test cases.
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 int a[100010],b[100010]; 5 LL C[100010],n; 6 map<int,int>M; 7 set<int>S; 8 set<int>::iterator it; 9 int lowbit(int x){ 10 return x&-x; 11 } 12 int sum(int x){ 13 LL ret=0; 14 while(x>0){ 15 ret+=C[x]; 16 x-=lowbit(x); 17 } 18 return ret; 19 } 20 void add(int x,int d){ 21 while(x<=n){ 22 C[x]+=d; 23 x+=lowbit(x); 24 } 25 } 26 int main(){ 27 int x,y,i,j,k; 28 while(cin>>n>>x>>y){ 29 memset(C,0,sizeof(C)); 30 M.clear(); 31 S.clear(); 32 LL ans=0; 33 for(i=1;i<=n;++i){ 34 scanf("%d",a+i); 35 b[i]=a[i]; 36 } 37 int tot=0; 38 sort(b+1,b+1+n); 39 for(i=1;i<=n;++i){ 40 if(M[b[i]]) continue; 41 M[b[i]]=++tot; 42 } 43 44 for(i=n;i>=1;--i){ 45 ans+=sum(M[a[i]]-1); 46 add(M[a[i]],1); 47 } 48 cout<<ans*min(x,y)<<endl; 49 } 50 return 0; 51 }
hdu多校(二) 1004 1007 1010