codeforces croc 2016 B. Mischievous Mess Makers (贪心)

题目链接 题意:长度为n的初始为1,2,3...n的序列,最多进行k次两个数交换,变换后的序列中最懂能有多少逆序对。 思路:贪心得想。。每次变最外层的对答案贡献最多。 以及,最能能变化n/2次。

/* ***********************************************
Author :111qqz
Created Time :2016年03月19日 星期六 00时21分20秒
File Name :code/cf/croc2016/B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6LL n,k;
 7int main()
 8{
 9	#ifndef  ONLINE_JUDGE 
10	freopen("code/in.txt","r",stdin);
11  #endif
12	cin>>n>>k;
13	LL cnt = min(n/2,k);
14	LL ans =  0;
15	LL cur = n-1;
16	for ( int i = 1 ; i <=cnt ; i++)
17	{
18	    ans += cur;
19	    cur--;
20	    ans +=cur;
21	    cur--;
22	}
23	cout<<ans<<endl;
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}