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

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

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