# PKU(1455, 2140, 3627, 3664)

1455, 2140, 3627, 3664

#### PKU 1455

n participants of << crazy tea party >> sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required for all participants to sit in reverse order (so that left neighbors would become right, and right - left).

participants 参加者

```int dp;

int main(){
int n,m,a,b;

dp = 1;
REP(i,2,32768){
a = i/2;
b = i-a;
dp[i] = a*(a-1)/2 + b*(b-1)/2;
}

cin>>n;
while(n--){
cin>>m;
printf("%d\n",dp[m]);
}
return 0;
}
```

#### PKU 2140

The cows in farmer John's herd are numbered and branded with consecutive integers from 1 to N (1 <= N <= 10,000,000). When the cows come to the barn for milking, they always come in sequential order from 1 to N.
Farmer John, who majored in mathematics in college and loves numbers, often looks for patterns. He has noticed that when he has exactly 15 cows in his herd, there are precisely four ways that the numbers on any set of one or more consecutive cows can add up to 15 (the same as the total number of cows). They are: 15, 7+8, 4+5+6, and 1+2+3+4+5.
When the number of cows in the herd is 10, the number of ways he can sum consecutive cows and get 10 drops to 2: namely 1+2+3+4 and 10.
Write a program that will compute the number of ways farmer John can sum the numbers on consecutive cows to equal N. Do not use precomputation to solve this problem.

consecutive 連続的な
barn 畜舎
precomputation 事前計算

```int main(){
int n,m,k;
double d;

cin>>n;
d = sqrt((double)2.0*n);
m = d;

int ans = 1;

REP(i,2,m+1){
int a = n/i - (i-1)/2+i-1;
int b = n/i - (i-1)/2-1;
int tmp = a*(a+1)/2 - b*(b+1)/2;
if( tmp==n )ans++;
}
printf("%d\n",ans);

return 0;
}
```

#### PKU 3627

Farmer John recently bought a bookshelf for cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.
Each of the N cows (1 ≤ N ≤ 20,000) has some height of Hi (1 ≤ Hi ≤ 10,000) and a total height summed across all N cows of S. The bookshelf has a height of B (1 ≤ B ≤ S < 2,000,000,007).
To reach the top of the bookshelf taller than the tallest cow, one or more of the cows can stand on top of each other in a stack, so that their total height is the sum of each of their individual heights. This total height must be no less than the height of the bookshelf. Since more cows than necessary in the stack can be dangerous, your job is to find the set of cows that produces a stack of the smallest number of cows possible such that the stack can reach the bookshelf.

bookshelf 本棚

```int main(){
int N,H,h,sum;
vector<int> vi;

cin>>N>>H;
rep(i,N){
cin>>h;
vi.push_back(h);
}
sort( vi.rbegin(), vi.rend() );

sum = 0;
int i;
for(i=0; i<N; i++){
sum += vi[i];
if( sum >= H )break;
}
printf("%d\n",i+1);

return 0;
}
```

#### PKU 3664

The cows are having their first election after overthrowing the tyrannical Farmer John, and Bessie is one of N cows (1 ≤ N ≤ 50,000) running for President. Before the election actually happens, however, Bessie wants to determine who has the best chance of winning.
The election consists of two rounds. In the first round, the K cows (1 ≤ K ≤ N) cows with the most votes advance to the second round. In the second round, the cow with the most votes becomes President.
Given that cow i expects to get Ai votes (1 ≤ Ai ≤ 1,000,000,000) in the first round and Bi votes (1 ≤ Bi ≤ 1,000,000,000) in the second round (if he or she makes it), determine which cow is expected to win the election. Happily for you, no vote count appears twice in the Ai list; likewise, no vote count appears twice in the Bi list.

election 選挙
overthrowing 転覆
tyrannical 非道の
expect 期待
likewise 同様に

```struct _cow{
int a,b,ind;
_cow(int _a, int _b, int _ind){
a=_a; b=_b; ind=_ind;
}
};

bool cmp1( _cow _a, _cow _b ){
return _a.a > _b.a;
}
bool cmp2( _cow _a, _cow _b ){
return _a.b > _b.b;
}
int main(){
int n,k,a,b;

vector< _cow > vp;

cin>>n>>k;
rep(i,n){
scanf("%d %d",&a,&b);
vp.push_back( _cow(a,b,i+1) );
}
sort( vp.begin(), vp.end(), cmp1 );
sort( vp.begin(), vp.begin()+k, cmp2 );

cout << vp.ind << endl;

return 0;
}
```